c语言json配置文件linux,Linux下C语言操作json数据

本文最后更新于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=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值