下载地址:https://sourceforge.net/projects/cjson/
1,添加到工程中就可直接使用了
2,把堆和栈弄大一点;
3应用
typedef struct
{
char firstName[32];
char lastName[32];
char email[64];
int age;
float height;
} PERSON;
/* a bunch of json: */
char text1[]="{\"firstName\":\"Brett\"}";
char text2[]="{\"person\":{\"firstName\":\"z\",\"lastName\":\"jadena\",\"email\":\"jadena@126.com\",\"age\":8,\"height\":1.17}}";
void doit(char *text)
{
char *out;
cJSON *root;
cJSON*item;
cJSON *object;
PERSON person;
root=cJSON_Parse(text);
if (!root) {u3_printf("Error before: [%s]\r\n",cJSON_GetErrorPtr());}
else
{
out=cJSON_Print(root);
u3_printf("%s\r\n",out);
object=cJSON_GetObjectItem(root,"person");
if(!object)
{
u3_printf("object Error before: [%s]\r\n",cJSON_GetErrorPtr());
}
else
{
item=cJSON_GetObjectItem(object,"firstName");
memcpy(person.firstName,item->valuestring,strlen(item->valuestring));
item=cJSON_GetObjectItem(object,"lastName");
memcpy(person.lastName,item->valuestring,strlen(item->valuestring));
item=cJSON_GetObjectItem(object,"email");
memcpy(person.email,item->valuestring,strlen(item->valuestring));
item=cJSON_GetObjectItem(object,"age");
person.age=item->valueint;
item=cJSON_GetObjectItem(object,"height");
person.height=item->valuedouble;
u3_printf("firstName:%s\r\n",person.firstName);
u3_printf("lastName:%s\r\n",person.lastName);
u3_printf("email:%s\r\n",person.email);
u3_printf("age:%d\r\n",person.age);
u3_printf("height:%f\r\n",person.height);
}
cJSON_Delete(root);
// u3_printf("%s\r\n",out);
free(out);
}
}
void main(void)
{
doit(text1);
doit(text2);
}