#include
#include
#include
#include
#include
/*
{
"name": "tom",
"age": 10,
"mobile": null,
"man": true,
"bike": {
"branch": "yongjiu"
},
"score": [90, 20, 30, 40],
"shuju": {
"status": 1,
"data": [{
"fuck1": "fk1",
"hard1": "hd1"
}, {
"fuck2": "fk2",
"hard2": "hd2"
}]
}
}
*/
void parse(char* jsonbuf)
{
cJSON* root = cJSON_Parse(jsonbuf);
cJSON* name = cJSON_GetObjectItem(root, "name");
printf("%s=%s\n", name->string, name->valuestring);
cJSON* age = cJSON_GetObjectItem(root, "age");
printf("%s=%g\n", age->string, age->valuedouble);
cJSON* mobile = cJSON_GetObjectItem(root, "mobile");
printf("%d\n", mobile->type);
cJSON*bike = cJSON_GetObjectItem(root, "bike");
cJSON* branch = cJSON_GetObjectItem(bike, "branch");
cJSON* score = cJSON_GetObjectItem(root, "score");
cJSON* score2 = cJSON_GetArrayItem(score, 1);
printf("score2 = %g\n", score2->valuedouble);
cJSON* shuju = cJSON_GetObjectItem(root, "shuju");
cJSON* status = cJSON_GetObjectItem(shuju, "status");
printf("%s=%g\n", status->string, status->valuedouble);
}
int main()
{
/*
{
"name": "tom",
"age": 10,
"mobile": null,
"man": true,
"bike": {
"branch": "yongjiu"
},
"score": [90, 20, 30, 40],
"shuju": {
"status": 1,
"data": [{
"fuck1": "fk1",
"hard1": "hd1"
}, {
"fuck2": "fk2",
"hard2": "hd2"
}]
}
}
*/
cJSON* root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "name", "tom");
cJSON_AddNumberToObject(root, "age", 10);
cJSON_AddNullToObject(root, "mobile");
cJSON_AddBoolToObject(root, "man", 1);
cJSON* bike = cJSON_CreateObject();
cJSON_AddStringToObject(bike, "branch", "yongjiu");
cJSON_AddItemToObject(root, "bike", bike);
cJSON* score = cJSON_CreateArray();
cJSON_AddItemToArray(score, cJSON_CreateNumber(90));
cJSON_AddItemToArray(score, cJSON_CreateNumber(20));
cJSON_AddItemToArray(score, cJSON_CreateNumber(30));
cJSON_AddItemToArray(score, cJSON_CreateNumber(40));
cJSON_AddItemToObject(root, "score", score);
cJSON* shuju = cJSON_CreateObject();
cJSON_AddNumberToObject(shuju,"status",1);
cJSON* data = cJSON_CreateArray();
cJSON* s1 = cJSON_CreateObject();
cJSON_AddStringToObject(s1, "fuck1", "fk1");
cJSON_AddStringToObject(s1, "hard1", "hd1");
cJSON_AddItemToArray(data,s1);
cJSON* s2 = cJSON_CreateObject();
cJSON_AddStringToObject(s2, "fuck2", "fk2");
cJSON_AddStringToObject(s2, "hard2", "hd2");
cJSON_AddItemToArray(data,s2);
cJSON_AddItemToObject(shuju,"data",data);
// cJSON_AddItemToArray(datas, "data",data);
cJSON_AddItemToObject(root,"shuju",shuju);
char* datas = cJSON_Print(root);
printf("%s\n", datas);
/*
{
"name": "tom",
"age": 10,
"mobile": null,
"man": true,
"bike": {
"branch": "yongjiu"
},
"score": [90, 20, 30, 40],
"shuju": {
"status": 1,
"data": [{
"fuck1": "fk1",
"hard1": "hd1"
}, {
"fuck2": "fk2",
"hard2": "hd2"
}]
}
}
*/
parse(datas);
}
下面是运行结果
abcd@abcd-virtual-machine:~/curl$ gcc 复杂的拼接json.c -lcjson
abcd@abcd-virtual-machine:~/curl$ ./a.out
{
"name": "tom",
"age": 10,
"mobile": null,
"man": true,
"bike": {
"branch": "yongjiu"
},
"score": [90, 20, 30, 40],
"shuju": {
"status": 1,
"data": [{
"fuck1": "fk1",
"hard1": "hd1"
}, {
"fuck2": "fk2",
"hard2": "hd2"
}]
}
}
name=tom
age=10
4
score2 = 20
status=1
abcd@abcd-virtual-machine:~/curl$