#include<stdio.h>
#include<curl/curl.h>
#include <cJSON/cJSON.h>
#include <stdlib.h>
#include <string.h>
/*
{
"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);
// printf("%g\n", mobile->valuestring);
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);
cJSON* myarr = cJSON_GetObjectItem(shuju,"data");
if(myarr)
{
int size = cJSON_GetArraySize(myarr);
printf("%d\n",size);
int ii;
for(ii=0;ii<size;ii++)
{
cJSON* childNode = cJSON_GetArrayItem(myarr, ii);
if(childNode)
{
cJSON* child = cJSON_GetObjectItem(childNode,"fuck");
if(child)
{
//printf("Get child name : %s\n", child->valuestring);
printf("%s=%s\n", child->string, child->valuestring);
}
cJSON* child1 = cJSON_GetObjectItem(childNode,"hard");
if(child1)
{
//printf("Get child name : %s\n", child->valuestring);
printf("%s=%s\n", child1->string, child1->valuestring);
}
}
}
}
}
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, "fuck", "fk1");
cJSON_AddStringToObject(s1, "hard", "hd1");
cJSON_AddItemToArray(data,s1);
cJSON* s2 = cJSON_CreateObject();
cJSON_AddStringToObject(s2, "fuck", "fk2");
cJSON_AddStringToObject(s2, "hard", "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": [{
"fuck": "fk1",
"hard": "hd1"
}, {
"fuck": "fk2",
"hard": "hd2"
}]
}
}
name=tom
age=10
4
score2 = 20
status=1
2
fuck=fk1
hard=hd1
fuck=fk2
hard=hd2
abcd@abcd-virtual-machine:~/curl$