本文最后更新于2017年8月19日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!
=Start=
缘由:
总结、提高需要
正文:
参考解答:
#include
#include
/*
{
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
#include
#include
#include
#include
#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();
参考链接:
=END=