zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    MySQL行(记录)的详细操作一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理
    {MySQL完整性约束}一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业
    { MySQL基础数据类型}一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型
    MySQL的sql_mode模式说明及设置
    {MySQL存储引擎介绍}一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用
    {MySQL的库、表的详细操作}一 库操作 二 表操作 三 行操作
    小爬爬2.数据解析
    小爬爬2:fiddler安装和了解fiddler
    小爬爬2.回顾
    小爬爬1.requests基础操作
  • 原文地址:https://www.cnblogs.com/yeshenmeng/p/12720327.html
Copyright © 2011-2022 走看看