Table of Contents
C json-c
간단한 json 파싱 라이브러리이다.
아주 단순하면서도 잘 작동한다.
다만 함수를 잘못 호출했을 때 에러조차 없이 프로그램이 죽어버리는 버그가 있다.
install
git clone https://github.com/json-c/json-c.git
cd json-c
mkdir build
cd build
cmake ..
make
make test
sudo make install
sudo ldconfig
사용법
사용법은 아래 소스를 참조하고,
더 필요한 함수가 있으면 헤더 파일 찾아보는 것이 빠르다.
vi test.c
#include <stdio.h>
#include <json-c/json.h>
int main() {
char *buf = "{ \"aaa\" : \"sss\", \"bbb\" : \"111\", \"phoneNumber\": [ \"010-0000-0000\", \"010-1111-1111\", \"010-2222-2222\" ] }";
// read
json_object *rootObj = json_tokener_parse(buf);
// string
json_object *obj = json_object_object_get(rootObj, "aaa");
const char *str = json_object_get_string(obj);
printf("%s\n", str);
// int
obj = json_object_object_get(rootObj, "bbb");
int ival = json_object_get_int(obj);
printf("%i\n", ival);
// json-c/json.h 를 열어보면 더많은 타입을 확인할 수 있다.
// json_object_object_foreach 를 이용해 key-value 방식의 배열도 처리할 수 있다.
json_object *phoneNumber = json_object_object_get(rootObj, "phoneNumber");
if (json_object_is_type(phoneNumber, json_type_array) == 1) {
for (int i = 0; i < json_object_array_length(phoneNumber); i++) {
obj = json_object_array_get_idx(phoneNumber, i);
str = json_object_get_string(obj);
printf("%s\n", str);
}
}
// write
json_object *newRootObj = json_object_new_object();
json_object_object_add(newRootObj, "aaa", json_object_new_string("111"));
printf("%s\n", json_object_get_string(newRootObj));
return 0;
}
gcc test.c -ljson-c
./a.out