cJson使用的简单例子

cJson 结构体简要说明

/* The cJSON structure: */
typedef struct cJSON {
    struct cJSON *next,*prev;   /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *child;        /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

    int type;                   /* The type of the item, as above. */

    char *valuestring;          /* The item's string, if type==cJSON_String */
    int valueint;               /* The item's number, if type==cJSON_Number */
    double valuedouble;         /* The item's number, if type==cJSON_Number */

    char *string;               /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;

说明:
1、cJSON是使用链表来存储数据的,其访问方式很像一颗树。每一个节点可以有兄弟节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。只有节点是对象或数组时才可以有孩子节点。
2、type是键(key)的类型,一共有7种取值,分别是:False,Ture,NULL,Number,String,Array,Object。
若是Number类型,则valueint或valuedouble中存储着值。若期望的是int,则访问valueint,若期望的是double,则访问valuedouble,可以得到值。
若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。
3、string中存放的是这个节点的名字,可理解为key的名称。

cJson 创建jsonObject并解析

//测试Json的结构体
struct JsonTest{

    int id;

    char cName[32];

    float  fValue;


};
void TestJsonObject(){

    //测试cJson 创建Json对象 

    JsonTest jsonObject = { 1, "geekCode", 0.2f };
    cJSON *root, *object;
    char *result;//生成的结果
    int i;

    root = cJSON_CreateObject();

    object = cJSON_CreateObject();
    cJSON_AddNumberToObject(object, "id", jsonObject.id);
    cJSON_AddStringToObject(object, "name", jsonObject.cName);
    cJSON_AddNumberToObject(object, "value", jsonObject.fValue);

    cJSON_AddNumberToObject(root, "id", jsonObject.id);
    cJSON_AddStringToObject(root, "name", jsonObject.cName);
    cJSON_AddNumberToObject(root, "value", jsonObject.fValue);
    cJSON_AddItemToObject(root, "object", object);

    result = cJSON_Print(root);
    cJSON_Delete(root);
    printf("生成Json:\n%s\n", result);

    //解析Json对象

    root = cJSON_Parse(result);
    if (!root) {
        printf("Error before: [%s]\n", cJSON_GetErrorPtr());
        return ;
    }
    object = cJSON_GetObjectItem(root, "object");

    if (!object){

        cJSON_Delete(root);
        return ;
    }

    JsonTest objectJson;
    JsonTest rootJson;

    //解析item
    cJSON *item;
    printf("解析过程:\n");
    //解析到objectJson
    item = cJSON_GetObjectItem(object, "id");
    printf("Item: type=%d, key is %s, valueint=%d\n", item->type, item->string, item->valueint);
    objectJson.id = item->valueint;
    item = cJSON_GetObjectItem(object, "name");
    printf("Item: type=%d, key is %s, valuestring=%s\n", item->type, item->string, item->valuestring);
    strcpy(objectJson.cName, item->string);
    item = cJSON_GetObjectItem(object, "value");
    printf("Item: type=%d, key is %s, valuedouble=%2f\n", item->type, item->string, item->valuedouble);
    objectJson.fValue = item->valuedouble;

    //解析到rootJson
    item = cJSON_GetObjectItem(root, "id");
    rootJson.id = item->valueint;
    item = cJSON_GetObjectItem(root, "name");
    strcpy(rootJson.cName, item->string);
    item = cJSON_GetObjectItem(root, "value");
    rootJson.fValue = item->valuedouble;

    //打印解析结果
    printf("解析结果:\nroot:\n");
    printf("\n\tid:%d\n\tname:%s\n\tvalue:%2f\n\t\n", rootJson.id, rootJson.cName, rootJson.fValue);
    printf("\tobject:\n\t\tid:%d\n\t\tname:%s\n\t\tvalue:%2f\n", objectJson.id, objectJson.cName, objectJson.fValue);

}
int main(){
//测试创建并解析jsonObject
TestJsonObject();
}

测试结果:
这里写图片描述

cJson 创建jsonArray并解析

void TestJsonArray(){

    //测试cJson 创建JsonArray数组
    JsonTest jsonObject = { 1, "geekCode", 0.2f };
    cJSON *root;
    char *result;//生成的结果
    const char *names[3] = { "zhang1", "zhagn2", "zhagng3" };

    root = cJSON_CreateObject();

    cJSON *JsonArray = cJSON_CreateArray();

    for (int i = 0; i < 3; i++){

        cJSON *ArrayItem = cJSON_CreateObject();
        jsonObject.id = i;
        cJSON_AddNumberToObject(ArrayItem, "id", jsonObject.id);
        cJSON_AddStringToObject(ArrayItem, "name", jsonObject.cName);
        cJSON_AddNumberToObject(ArrayItem, "value", jsonObject.fValue);
        cJSON_AddItemToArray(JsonArray, ArrayItem);
    }


    cJSON_AddNumberToObject(root, "id", jsonObject.id);
    cJSON_AddStringToObject(root, "name", jsonObject.cName);
    cJSON_AddNumberToObject(root, "value", jsonObject.fValue);
    cJSON_AddItemToObject(root, "JsonArray", JsonArray);
    cJSON_AddItemToObject(root, "names", cJSON_CreateStringArray(names, 3));

    //可以创建一下基础类型的json数组
    //cJSON *cJSON_CreateIntArray(const int *numbers, int count)        
    //cJSON *cJSON_CreateFloatArray(const float *numbers, int count)
    //cJSON *cJSON_CreateDoubleArray(const double *numbers, int count)  
    //cJSON *cJSON_CreateStringArray(const char **strings, int count)

    result = cJSON_Print(root);
    cJSON_Delete(root);
    printf("生成JsonArray:\n%s\n", result);

    //解析jsonArray
    root = cJSON_Parse(result);
    if (!root){
        printf("Error before: [%s]\n",cJSON_GetErrorPtr());
        return;
    }
    //解析到rootJson

    cJSON *item;
    JsonTest rootJson;
    item = cJSON_GetObjectItem(root, "id");
    rootJson.id = item->valueint;
    item = cJSON_GetObjectItem(root, "name");
    strcpy(rootJson.cName, item->string);
    item = cJSON_GetObjectItem(root, "value");
    rootJson.fValue = item->valuedouble;

    //解析jsonArray
    cJSON *Array;
    Array = cJSON_GetObjectItem(root, "JsonArray");
    int size = 0;
    size = cJSON_GetArraySize(Array);
    JsonTest *JsonArrays = new JsonTest[size];
    for (int i=0; i < size; i++){
    cJSON *ArrayItem = cJSON_GetArrayItem(Array,i);

    cJSON *ArrayValue = cJSON_GetObjectItem(ArrayItem, "id");
    JsonArrays[i].id = ArrayValue->valueint;

    ArrayValue = cJSON_GetObjectItem(ArrayItem, "name");
    strcpy(JsonArrays[i].cName, ArrayValue->string);

    ArrayValue = cJSON_GetObjectItem(ArrayItem, "value");
    JsonArrays[i].fValue = ArrayValue->valuedouble;

    }

    //打印解析结果
    printf("解析结果:\nroot:\n");
    printf("\n\tid:%d\n\tname:%s\n\tvalue:%2f\n\t\n", rootJson.id, rootJson.cName, rootJson.fValue);
    printf("\tJsonArray:\n");
    for (int i = 0; i < size; i++){
        printf("\t\tid:%d\n\t\tname:%s\n\t\tvalue:%2f\n", JsonArrays[i].id, JsonArrays[i].cName, JsonArrays[i].fValue);
        printf("\t\t------\n");
    }



}
int main(){
//测试创建并解析jsonObject
TestJsonObject();
}

测试结果
这里写图片描述

希望对您有所帮助!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值