cjson 下载链接 https://github.com/DaveGamble/cJSON
下载完成将其中的cJson.c cJson.h复制到自己的项目中引用即可;
1.字符串转json格式
cJSON *json = cJSON_Parse(str)
a.字符串指针类型
b.返回的cJSON指针类型
2.在指定的cJSON通过key查找value
const char *str = cJSON_Print(cJSON_GetObjectItem(json, "age"))
a.json为原始cJSON类型指针,age为key,str为返回接收的char *
b.如果返回的value为int 等整型,可以调用atoi(const char * str)函数
int x = atoi(str);
cJSON_Print函数返回值的字符串会加双引号,解决方案为:
cJSON *json;cJSON *json;
json = cJSON_Parse(text);
cJSON_GetObjectItem(json, "Action")->valuestring
这样返回的值没有双引号就是key对应的字符串本身。
原文:https://www.cnblogs.com/bliss-/p/12377094.html