cJson实例解析

2017-01-13 08:17:25来源:CSDN作者:qq_27396861人点击

cJSON下载
1)可到gitbub上下载 地址:https://github.com/kbranigan/cJSON
2)只需将cJSON.h 和 cJSON.c 文件拷贝到所需使用的目录即可使用

实例

{     name: "tom",     age: 18,     man: true}
注:     1) key永远是string类型     2) 这里的18是双精度,在json里都用number来表示
创建cJSON对象以及解包
1)建立跟节点:root = { }
cJSON *root = cJSON_CreateObject();
2)往跟节点里添加数据:
//root = {name: "tom"}cJSON_AddItemToObject(root, "name", cJSON_CreateString("tom"));
//root = {name: "tom", age: 18}cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(18));
//root = {name: "tom", age: 18, man: true}
cJSON_AddItemToObject(root, "man", cJSON_CreateBool(1));
3)将json对象转换为字符串(转化为连续的空间方便传输)
char *p = cJSON_Print(root);
4)释放cJSON对象
cJSON_Delete(root);//释放完跟节点,子节点自动释放free(p);     //这个一定要释放,因为cJSON_Print是malloc内存的
5)将字符串类型json转换为cJSON数据结构, 解析
char *json_buffer;cJSON *root = cJSON_Parse(json_buffer);
6)解析出数据
cJSON *name = cJSON_GetObjectItem(root, "name");printf("name = %s/n", name->valuestring);     //name = tom
cJSON *age = cJSON_GetObjectItem(root, "age");printf("age = %s/n", age->valueint);     //age = 18
7)编译:要加上cJSON.c文件和数学库
gcc json_test.c cJSON.c -lm

例一(json嵌套)

{     name: "tom",     age: 18     mobile: {          branch: "apple",          version: 6     }}
代码:
#include "../h.h"#include "cJSON.h"
char *json_buffer;
void packet(){    cJSON *root = cJSON_CreateObject();    cJSON_AddItemToObject(root, "name", cJSON_CreateString("tom"));    cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(18));
    cJSON *mobile = cJSON_CreateObject();    cJSON_AddItemToObject(mobile, "branch", cJSON_CreateString("apple"));    cJSON_AddItemToObject(mobile, "version", cJSON_CreateNumber(6));
    cJSON_AddItemToObject(root, "mobile", mobile);
    json_buffer = cJSON_Print(root);
    printf("%s/n", json_buffer);
    cJSON_Delete(root);}
void unpacket(){    cJSON *root = cJSON_Parse(json_buffer);
    cJSON *name = cJSON_GetObjectItem(root, "name");    printf("name = %s/n", name->valuestring);
    cJSON *age = cJSON_GetObjectItem(root, "age");    printf("age = %d/n", age->valueint);
    cJSON *mobile = cJSON_GetObjectItem(root, "mobile");
    cJSON *branch = cJSON_GetObjectItem(mobile, "branch");    printf("branch = %s/n", branch->valuestring);
    cJSON *version = cJSON_GetObjectItem(mobile, "version");    printf("version = %d/n", version->valueint);}
int main(){    packet();     //打包
    unpacket();     //解包
    return 0;}

例二(数组)

{     name: "tom",     age: 18,     score: [91, 100, 80],     mobile: null}
代码:
#include "../h.h"#include "cJSON.h"
char *json_buffer;
void packet(){    cJSON *root = cJSON_CreateObject();    cJSON_AddItemToObject(root, "name", cJSON_CreateString("tom"));    cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(18));
    cJSON *score = cJSON_CreateArray();    cJSON_AddItemToArray(score, cJSON_CreateNumber(91));    cJSON_AddItemToArray(score, cJSON_CreateNumber(100));    cJSON_AddItemToArray(score, cJSON_CreateNumber(80));
    cJSON_AddItemToObject(root, "score", score);    cJSON_AddItemToObject(root,"mobile", cJSON_CreateNull());
    json_buffer = cJSON_Print(root);    cJSON_Delete(root);
    printf("%s/n", json_buffer);}
void unpacket(){    cJSON *root = cJSON_Parse(json_buffer);
    cJSON *name = cJSON_GetObjectItem(root, "name");    printf("name = %s/n", name->valuestring);
    cJSON *age = cJSON_GetObjectItem(root, "age");    printf("age = %d/n", age->valueint);
    cJSON *score = cJSON_GetObjectItem(root, "score");
    cJSON *s1 = cJSON_GetArrayItem(score, 0);    printf("s1 = %d/n", s1->valueint);    cJSON *s2 = cJSON_GetArrayItem(score, 1);    printf("s2 = %d/n", s2->valueint);    cJSON *s3 = cJSON_GetArrayItem(score, 2);    printf("s3 = %d/n", s3->valueint);}
int main(){    packet();
    unpacket();
    return 0;}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台