=Start=
缘由:
总结、提高需要
正文:
参考解答:
#include <stdio.h> #include <json-c/json.h> /* { Name: haha, Id: 101, Age: 21, info:{ number: 1, score: 91, type: 2, params: "" } } */ // gcc json-c_parse_string.c -ljson-c int main(int argc, char const *argv[]) { /* Declaring the json data's in json format. */ char buf[] = "{ \"Name\": \"haha\", \"Id\": 101, \"Age\": 21, \"info\": { \"number\": 1, \"score\": 91, \"type\": 2, \"params\": \"w\" } }"; /* Declaring the json_object. To pass the Json string to the newly created json_object. */ json_object *new_obj = json_tokener_parse(buf); if (!new_obj) return -1; json_object *val_obj = NULL; json_object *result = NULL; const char *str = NULL; /* To get the data's then we have to get to the specific node by using the below function. */ if( json_object_object_get_ex(new_obj, "Name", &val_obj) ) { str = json_object_get_string(val_obj); printf("Name: %s\n", str); } if( json_object_object_get_ex(new_obj, "Id", &val_obj) ) { str = json_object_get_string(val_obj); printf("Id: %s\n", str); } if( json_object_object_get_ex(new_obj, "Age", &val_obj) ) { str = json_object_get_string(val_obj); printf("Age: %s\n", str); } if( json_object_object_get_ex(new_obj, "info", &val_obj) ) { if( json_object_object_get_ex(val_obj, "number", &result) ) { printf("\tinfo -> number: %d\n", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "score", &result) ) { printf("\tinfo -> score: %d\n", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "type", &result) ) { printf("\tinfo -> type: %d\n", json_object_get_int(result)); } if( json_object_object_get_ex(val_obj, "params", &result) ) { printf("\tinfo -> params: %s\n", json_object_get_string(result)); } } json_object_put(new_obj); // to return the pointer to its originalobjects return 0; }
&
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include <json-c/json.h> #ifdef TEST_FORMATTED #define json_object_to_json_string(obj) json_object_to_json_string_ext(obj, sflags) #else /* no special define */ #endif // gcc json_object_to_json_string.c -ljson-c -DTEST_FORMATTED=2 int main(int argc, char **argv) { json_object *new_obj; #ifdef TEST_FORMATTED int sflags = TEST_FORMATTED; #endif new_obj = json_tokener_parse("/* more difficult test case */" "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }"); printf("new_obj.to_string() = %s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); return 0; }
json_object_new_object();
json_object_new_int();
json_object_new_string();
json_object_object_add();
json_object_to_json_string();
json_tokener_parse();
json_object_object_get_ex();
json_object_get_int();
json_object_get_string();
json_object_put();
参考链接:
json-c
https://github.com/json-c/json-c
https://github.com/json-c/json-c/tree/master/tests
https://json-c.github.io/json-c/json-c-0.12/doc/html/json__object_8h.html #官方文档
JSON C语言API整理 #nice
http://7player.cn/2015/01/03/json-c%E8%AF%AD%E8%A8%80api%E6%95%B4%E7%90%86/
Parsing JSON using C [closed]
https://stackoverflow.com/questions/6673936/parsing-json-using-c
How to extract value of json object key, value pairs using json-c
https://stackoverflow.com/questions/29483115/how-to-extract-value-of-json-object-key-value-pairs-using-json-c
=END=
《“Linux下C语言操作json数据”》 有 1 条评论
cJSON解析和生成JSON文件
https://www.yanbinghu.com/2019/08/04/21364.html
`
1. 前言
2. cJSON
3. 关键数据结构
4. 常用接口函数
5. JSON文件解析准备
6. 解析JSON文件
7. 写JSON
8. 总结
9. 附录
`