csjon实例(转)

时间:2020-04-17
本文章向大家介绍csjon实例(转),主要包括csjon实例(转)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
主要函数:

    【1】常用创建
    【创建JSON对象】cJSON *cJSON_CreateObject(void);
    【创建JSON数组】cJSON *cJSON_CreateArray(void);
    
    【2】常用添加
    【向对象中添加对象】voidcJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
    【向数组中添加对象】void cJSON_AddItemToArray(cJSON *array, cJSON *item);
    【向对象中添加数值】cJSON_AddNumberToObject(object,name,n)
            【向对象中添加字符串】 cJSON_AddStringToObject(object,name,s)

示例:
#include
#include "cJSON.h"

char *makeJson()
{
    cJSON *pJsonRoot  = NULL;
    cJSON *pIntArray  = NULL;
    cJSON *pCommArray = NULL;
    cJSON *pSubJson   = NULL;
    char   *p          = NULL;
    int intarr[5] = {0, 1, 2, 3, 4}; //整数数组


    pJsonRoot = cJSON_CreateObject(); //创建JSON对象

    if(NULL == pJsonRoot)
    {
        //error happend here
        return NULL;
    }

    cJSON_AddStringToObject(pJsonRoot, "hello", "hello world"); //添加一个值为字符串的键值对"hello":"hello world"
    cJSON_AddNumberToObject(pJsonRoot, "number", 10010);        //添加一个值为数值的键值对"number": 10010
    cJSON_AddBoolToObject(pJsonRoot, "bool", 1);                //添加一个值为布尔的键值对"bool": 1



    pSubJson = cJSON_CreateObject();     //创建另一个json对象作为子对象

    if(NULL == pSubJson)
    {
        // create object faild, exit
        cJSON_Delete(pJsonRoot);         //删除pJsonRoot 及其子对象
        return NULL;
    }

    cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string"); //添加一个值为布尔的键值对
    cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);                   //将对象pSubJson添加到pJsonRoot中,成为键值对 "subobj":pSubJson


    //数值数组
    pIntArray = cJSON_CreateIntArray(intarr, 5); //为intarr创建一个数值数组对象,
    cJSON_AddItemToObject(pJsonRoot, "IntArr", pIntArray);  //将对象pIntArray添加到pJsonRoot中,成为键值对 " IntArr ":pIntArray


    //通用数组

    pCommArray = cJSON_CreateArray();//创建数组对象
    //cJSON_AddItemToArray(cJSON *array, cJSON *item);//向数组中添加对象
    cJSON_AddNumberToObject(pCommArray, "num", 2);             //向数组中添加数值cJSON_AddNumberToObject(object,name,n)
    cJSON_AddStringToObject(pCommArray, "str", "hopeview");    //向对象中添加字符串 cJSON_AddStringToObject(object,name,s)


    cJSON_AddItemToObject(pJsonRoot, "pCommArray ", pCommArray);  //将对象pCommArray添加到pJsonRoot中,成为键值对 " pCommArray ":pCommArray


    p = cJSON_Print(pJsonRoot);   //json对象转为json字符串用于传输

    // else use :
    // char * p = cJSON_PrintUnformatted(pJsonRoot);
    if(NULL == p)
    {
        //convert json list to string faild, exit
        //because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
        cJSON_Delete(pJsonRoot); //删除pJsonRoot 及其子对象
        return NULL;
    }

    //free(p);

    cJSON_Delete(pJsonRoot); //释放json对象
    printf("myJson is:%s",p);
    return p;               //返回json字符串,注意外面用完p要记得释放空间。//free(p);
}

原文链接:http://blog.sina.com.cn/s/blog_4c0cb1c00102xg7j.html

原文地址:https://www.cnblogs.com/yeshenmeng/p/12720327.html