목차
- 이전 포스팅의 C 언어 Json 문자열 파싱 후 Json 객체 생성 예제 학습 링크
- 이전 포스팅의 C 언어 Opencv 라이브러리로 사각형, 원형 그리기 예제 학습 링크
- 이전 포스팅의 C 언어 Opencv 활용 Jpeg 이미지 좌우 전환하기 예제 학습 링크
- C 언어 이중 연결 리스트로 Json Array 배열 생성 예제
- C 언어 이중 연결 리스트 탐색 후 Json Array 내용 출력 예제
이전 포스팅의 C 언어 Json 문자열 파싱 후 Json 객체 생성 예제 학습 링크
이전 포스팅에서는 c언어에서 json 문자열을 파싱하여 json context 객체를 다시 생성하는 예제에 대해 알아보았습니다. json 은 키와 값을 쌍으로 저장하고 관리할 수 있는 자료구조입니다. 보통 네트워크 통신을 위해 사용됩니다. 대표적인 사용 프로토콜로서는 HTTP 프로토콜이 있습니다. 클라이언트와 서버간 데이터를 주고받기 위해 키와 값을 저장하여 전송합니다. 이 때 바이트화를 Serialization 즉, 시리얼화 한다고 표현하기도 합니다. 구조화되어있는 데이터들을 일련으로 변환하여 문자열로 변환 후 상대방에게 전송합니다. 만약 c언어에서 이러한 json 문자열 파싱 방법이 궁금하시다면 아래 링크를 참고하여 학습해주세요.
2023.11.13 - [C] - [C 언어/C++] C 언어 포인터로 Json 자료구조 만들기 및 활용 예제 - 3(json 문자열 파싱 및 context 생성 예제)
[C 언어/C++] C 언어 포인터로 Json 자료구조 만들기 및 활용 예제 - 3(json 문자열 파싱 및 context 생성
목차 이전 포스팅의 C 언어 json 자료구조 선언 후 키와 값 저장 예제 학습 링크 이전 포스팅의 C 언어 더블 포인터 Double Pointer 활용 예제 학습 링크 이전 포스팅의 C 언어 Codelite IDE에 opencv 라이브
salguworld.tistory.com
이전 포스팅의 C 언어 Opencv 라이브러리로 사각형, 원형 그리기 예제 학습 링크
다음으로 이전 포스팅에서는 c언어에서 opencv 라이브러리를 사용하여 사각형, 원형 등의 도형을 생성하여 화면에 출력하는 예제에 대해 알아보았습니다. 일반적으로 도형이란 점으로 이루어진 것들을 선으로 연결하여 만든 shape를 의미합니다. 특히 c언어에서 이러한 도형을 구성하기위해서는 파이썬 자바와 다르게 opencv를 라이브러리를 활용하면 조금더 원활히 진행할 수 있습니다. 만약 수학적 기능을 만들고 있거나 c언어로 도형 구성이 필요하시다면 아래의 포스팅을 참고하여 학습해주세요.
2023.09.07 - [C] - [C/C++] C++ 언어 opencv 활용 사각형, 원형 그리기 예제
[C/C++] C++ 언어 opencv 활용 사각형, 원형 그리기 예제
목차 C++ 언어 Codelite에 opencv 라이브러리 링크 및 개발 환경 구성 예제 이전 포스팅 참고 학습 C 언어 fopen 파일 생성 및 스트림 열기 예제 이전 포스팅 참고 학습 C 언어 fclose 파일 스트림 닫기 예
salguworld.tistory.com
이전 포스팅의 C 언어 Opencv 활용 Jpeg 이미지 좌우 전환하기 예제 학습 링크
마지막으로 이전 포스팅에서는 c언어에서 opencv 라이브러리를 사용하여 로컬에 저장된 JPEG 이미지를 좌우 전환하는 방법과 예제에 대해 알아보았습니다. 좌우 전환이란 왼쪽에서 오른쪽으로 진행되는 사진을 반대 방향으로 바꾸는 것을 의미합니다. 일반적으로 이미지 편집 프로그램을 사용하면 쉽게 전환할 수 있지만, 내가 이미지 편집 프로그램을 c언어로 개발하고 있거나 이미지 전환 자동화 프로그램을 c언어로 개발중이라면 이러한 예제가 도움될 수 있습니다. 만약 c언어에서 opencv라이브러리로 이미지 좌우 전환 방법이 궁금하시다면 아래 링크를 참고하여 학습해주세요.
2023.09.08 - [C] - [C/C++] C 언어 opencv 활용 jpeg 이미지 좌우 전환 출력 예제
[C/C++] C 언어 opencv 활용 jpeg 이미지 좌우 전환 출력 예제
목차 C 언어 Codelite에 opencv 라이브러리 링크 및 개발 환경 구성 예제 이전 포스팅 참고 학습 C 언어 opencv 라이브러리 활용 도형 마우스 드래그로 이동 시키기 예제 이전 포스팅 참고 학습 C 언어 op
salguworld.tistory.com
C 언어 이중 연결 리스트로 Json Array 배열 생성 예제
아래는 c언어에서 이중 연결 리스트로 Json Array 자료구조를 생성하는 예제입니다. 이중 연결 리스트는 이전 노드와 자기 자신의 다음 노드의 포인터를 기억함으로써 조금더 효율적인 자료 관리가 가능토록 도와주는 자료구조입니다. 이 이중 연결 리스트로 Json Array 자료구조를 만들 수 있습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Json 객체를 나타내는 구조체
typedef struct JsonListNode {
char* key;
char* value;
struct JsonListNode* next;
struct JsonListNode* prev;
} JsonListNode;
// Json 객체를 관리하는 구조체
typedef struct {
JsonListNode* head;
JsonListNode* tail;
} JsonObject;
// Json Array를 나타내는 구조체
typedef struct JsonArrayNode {
JsonObject obj;
struct JsonArrayNode* next;
struct JsonArrayNode* prev;
} JsonArrayNode;
// Json Array를 관리하는 구조체
typedef struct {
JsonArrayNode* head;
JsonArrayNode* tail;
} JsonArray;
// Json 객체를 초기화하는 함수
void initializeJsonObject(JsonObject* jsonObj) {
jsonObj->head = NULL;
jsonObj->tail = NULL;
}
// Json 객체에 새로운 키와 값을 추가하는 함수
void addJsonEntry(JsonObject* jsonObj, const char* key, const char* value) {
JsonListNode* newNode = (JsonListNode*)malloc(sizeof(JsonListNode));
newNode->key = strdup(key);
newNode->value = strdup(value);
newNode->next = NULL;
if (jsonObj->tail == NULL) {
jsonObj->head = newNode;
jsonObj->tail = newNode;
newNode->prev = NULL;
} else {
jsonObj->tail->next = newNode;
newNode->prev = jsonObj->tail;
jsonObj->tail = newNode;
}
}
// Json Array를 초기화하는 함수
void initializeJsonArray(JsonArray* jsonArray) {
jsonArray->head = NULL;
jsonArray->tail = NULL;
}
// Json Array에 새로운 객체를 추가하는 함수
void addJsonObject(JsonArray* jsonArray, const JsonObject* jsonObj) {
JsonArrayNode* newNode = (JsonArrayNode*)malloc(sizeof(JsonArrayNode));
newNode->obj = *jsonObj;
newNode->next = NULL;
if (jsonArray->tail == NULL) {
jsonArray->head = newNode;
jsonArray->tail = newNode;
newNode->prev = NULL;
} else {
jsonArray->tail->next = newNode;
newNode->prev = jsonArray->tail;
jsonArray->tail = newNode;
}
}
// 메모리 해제 함수
void freeJsonArray(JsonArray* jsonArray) {
JsonArrayNode* currentNode = jsonArray->head;
JsonArrayNode* nextNode;
while (currentNode != NULL) {
nextNode = currentNode->next;
JsonListNode* currentEntry = currentNode->obj.head;
JsonListNode* nextEntry;
while (currentEntry != NULL) {
nextEntry = currentEntry->next;
free(currentEntry->key);
free(currentEntry->value);
free(currentEntry);
currentEntry = nextEntry;
}
free(currentNode);
currentNode = nextNode;
}
jsonArray->head = NULL;
jsonArray->tail = NULL;
}
// 예제 사용
int main() {
// 첫 번째 JSON 객체 생성
JsonObject json1;
initializeJsonObject(&json1);
addJsonEntry(&json1, "name", "wonseok");
addJsonEntry(&json1, "age", "24");
// 두 번째 JSON 객체 생성
JsonObject json2;
initializeJsonObject(&json2);
addJsonEntry(&json2, "city", "Seoul");
addJsonEntry(&json2, "country", "South Korea");
// Json Array 생성 및 객체 추가
JsonArray myJsonArray;
initializeJsonArray(&myJsonArray);
addJsonObject(&myJsonArray, &json1);
addJsonObject(&myJsonArray, &json2);
// 메모리 해제
freeJsonArray(&myJsonArray);
return 0;
}
C 언어 이중 연결 리스트 탐색 후 Json Array 내용 출력 예제
아래는 c언어에서 이중 연결 리스트에 저장된 모든 Json 객체들을 탐색하여 내용을 출력하는 예제입니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Json 객체를 나타내는 구조체
typedef struct JsonListNode {
char* key;
char* value;
struct JsonListNode* next;
struct JsonListNode* prev;
} JsonListNode;
// Json 객체를 관리하는 구조체
typedef struct {
JsonListNode* head;
JsonListNode* tail;
} JsonObject;
// Json Array를 나타내는 구조체
typedef struct JsonArrayNode {
JsonObject obj;
struct JsonArrayNode* next;
struct JsonArrayNode* prev;
} JsonArrayNode;
// Json Array를 관리하는 구조체
typedef struct {
JsonArrayNode* head;
JsonArrayNode* tail;
} JsonArray;
// Json 객체를 초기화하는 함수
void initializeJsonObject(JsonObject* jsonObj) {
jsonObj->head = NULL;
jsonObj->tail = NULL;
}
// Json 객체에 새로운 키와 값을 추가하는 함수
void addJsonEntry(JsonObject* jsonObj, const char* key, const char* value) {
JsonListNode* newNode = (JsonListNode*)malloc(sizeof(JsonListNode));
newNode->key = strdup(key);
newNode->value = strdup(value);
newNode->next = NULL;
if (jsonObj->tail == NULL) {
jsonObj->head = newNode;
jsonObj->tail = newNode;
newNode->prev = NULL;
} else {
jsonObj->tail->next = newNode;
newNode->prev = jsonObj->tail;
jsonObj->tail = newNode;
}
}
// Json Array를 초기화하는 함수
void initializeJsonArray(JsonArray* jsonArray) {
jsonArray->head = NULL;
jsonArray->tail = NULL;
}
// Json Array에 새로운 객체를 추가하는 함수
void addJsonObject(JsonArray* jsonArray, const JsonObject* jsonObj) {
JsonArrayNode* newNode = (JsonArrayNode*)malloc(sizeof(JsonArrayNode));
newNode->obj = *jsonObj;
newNode->next = NULL;
if (jsonArray->tail == NULL) {
jsonArray->head = newNode;
jsonArray->tail = newNode;
newNode->prev = NULL;
} else {
jsonArray->tail->next = newNode;
newNode->prev = jsonArray->tail;
jsonArray->tail = newNode;
}
}
// Json Array의 내용을 출력하는 함수
void printJsonArrayContents(const JsonArray* jsonArray) {
JsonArrayNode* currentNode = jsonArray->head;
while (currentNode != NULL) {
JsonListNode* currentEntry = currentNode->obj.head;
printf("Json Object:\n");
while (currentEntry != NULL) {
printf(" Key: %s, Value: %s\n", currentEntry->key, currentEntry->value);
currentEntry = currentEntry->next;
}
currentNode = currentNode->next;
}
}
// 메모리 해제 함수
void freeJsonArray(JsonArray* jsonArray) {
JsonArrayNode* currentNode = jsonArray->head;
JsonArrayNode* nextNode;
while (currentNode != NULL) {
nextNode = currentNode->next;
JsonListNode* currentEntry = currentNode->obj.head;
JsonListNode* nextEntry;
while (currentEntry != NULL) {
nextEntry = currentEntry->next;
free(currentEntry->key);
free(currentEntry->value);
free(currentEntry);
currentEntry = nextEntry;
}
free(currentNode);
currentNode = nextNode;
}
jsonArray->head = NULL;
jsonArray->tail = NULL;
}
// 예제 사용
int main() {
// 첫 번째 JSON 객체 생성
JsonObject json1;
initializeJsonObject(&json1);
addJsonEntry(&json1, "name", "wonseok");
addJsonEntry(&json1, "age", "24");
// 두 번째 JSON 객체 생성
JsonObject json2;
initializeJsonObject(&json2);
addJsonEntry(&json2, "city", "Seoul");
addJsonEntry(&json2, "country", "South Korea");
// Json Array 생성 및 객체 추가
JsonArray myJsonArray;
initializeJsonArray(&myJsonArray);
addJsonObject(&myJsonArray, &json1);
addJsonObject(&myJsonArray, &json2);
// Json Array의 내용 출력
printf("Json Array Contents:\n");
printJsonArrayContents(&myJsonArray);
// 메모리 해제
freeJsonArray(&myJsonArray);
return 0;
}