zoukankan      html  css  js  c++  java
  • cJSON和rapidjson开源库解析数据

    1、

    rapidjson官网:http://rapidjson.org/zh-cn/

    /* 
    解析JSON数据 并将解析的数据转为 int型
    入参:    char* JSONInfo        待解析JSON数据
    char* Key            待解析的JSON中Key
    
    出参:    int* Value            解析出的Value值 
    返回值:0成功;非0失败
    */
    int  JSONtoInt(char* JSONInfo,char* Key,int* Value)
    {
        //strReplace(JSONInfo," ","");    //去除空格
        Document  jsonDoc;
        rapidjson::Value::ConstMemberIterator root;
        if(!jsonDoc.HasMember(Key) )
        {
            return 1;   //json does not contain this element
        }
        jsonDoc.Parse<rapidjson::kParseDefaultFlags>(JSONInfo);
        * Value = jsonDoc.FindMember(Key)->value.GetInt();
    
        /*
        cJSON * root = cJSON_Parse(JSONInfo);//将字符串格式的json数据转化为JSON对象格式
        if(root == NULL)
        {
            //MessageBox("parse error
    ");
            return 1 ;
        }
    
        cJSON* value = cJSON_GetObjectItem(root,Key);
        if( value == NULL )
        {
            //MessageBox("Get value error!");
            return 1;
        }
    
        char stmp[1024] = {0};
    
        sprintf(stmp,"%s",cJSON_Print(value));
        strReplace(stmp,""","");    //去除双引号
        strReplace(stmp," ","");    //去除空格
        *Value  = atoi(stmp);
        */
        
    
        return PB_OK;
    }
    
    /* 
    解析JSON数据 并将解析的数据转为 char*
    入参:    char* JSONInfo        待解析JSON数据
    char* Key            待解析的JSON中Key
    
    出参:    char* Value            解析出的Value值
    返回值:0成功;非0失败
    */
    int  JSONtoArray(char* JSONInfo,char* Key,char* Value)
    {
    
        
        //strReplace(JSONInfo," ","");    //去除空格
        Document  jsonDoc;
        rapidjson::Value::ConstMemberIterator root;
        jsonDoc.Parse<rapidjson::kParseDefaultFlags>(JSONInfo);
        if(!jsonDoc.HasMember(Key) )
        {
            return 1;   //json does not contain this element
        }
        CString aa = jsonDoc.FindMember(Key)->value.GetString();
        strcpy(Value,aa.GetBuffer());
        
        /*
    
        //cJSON 开源库是C的,在CPP会存在加载预编译等问题,会比较容易报错,移植性不好
        cJSON * root = cJSON_Parse(JSONInfo);//将字符串格式的json数据转化为JSON对象格式
        if(root == NULL)
        {
            //MessageBox("parse error
    ");
            return 1 ;
        }
    
    
        cJSON* value = cJSON_GetObjectItem(root,Key);
        if( value == NULL )
        {
            //MessageBox("Get value error!");
            return 1;
        }
    
        char stmp[1024] = {0};
    
        sprintf(stmp,"%s",cJSON_Print(value));
        strReplace(stmp,""","");    //去除双引号
        strReplace(stmp," ","");    //去除空格
        strcpy(Value,stmp);
        */
        
        return PB_OK;
    }
    View Code

    2、

    #include "cJSON.h"
    
    /* 
    解析JSON数据 并将解析的数据转为 int型
    入参:    char* JSONInfo        待解析JSON数据
    char* Key            待解析的JSON中Key
    
    出参:    int* Value            解析出的Value值     
    */
    int  JSONtoInt_cJSON(char* JSONInfo,char* Key,int* Value)
    {
        
        strReplace(JSONInfo," ","");        //去除空格
        cJSON * root = cJSON_Parse(JSONInfo);//将字符串格式的json数据转化为JSON对象格式
        if(root == NULL)
        {
            //MessageBox("parse error
    ");
            return 1 ;
        }
    
    
        cJSON* value = cJSON_GetObjectItem(root,Key);
        if( value == NULL )
        {
            //MessageBox("Get value error!");
            return 1;
        }
    
        char stmp[1024] = {0};
    
        sprintf(stmp,"%s",cJSON_Print(value));
        strReplace(stmp,""","");    //去除双引号
        strReplace(stmp," ","");    //去除空格
        *Value  = atoi(stmp);
        
        
    
        return 0;
    }
    
    /* 
    解析JSON数据 并将解析的数据转为 char*
    入参:    char* JSONInfo        待解析JSON数据
    char* Key            待解析的JSON中Key
    
    出参:    char* Value            解析出的Value值     
    */
    int  JSONtoArray_cJSON(char* JSONInfo,char* Key,char* Value)
    {
        
        cJSON * root = cJSON_Parse(JSONInfo);//将字符串格式的json数据转化为JSON对象格式
        if(root == NULL)
        {
            //MessageBox("parse error
    ");
            return 1 ;
        }
    
    
        cJSON* value = cJSON_GetObjectItem(root,Key);
        if( value == NULL )
        {
            //MessageBox("Get value error!");
            return 1;
        }
    
        char stmp[1024] = {0};
    
        sprintf(stmp,"%s",cJSON_Print(value));
        strReplace(stmp,""","");    //去除双引号
        strReplace(stmp," ","");    //去除空格
        strcpy(Value,stmp);
        
    
    
        return 0;
    
    }
    View Code
  • 相关阅读:
    【事件类】雅思口语
    【人物类】雅思口语
    【物品类】雅思口语
    Cassandra (二)Java | scala操作
    rpm安装在哪里了?
    用IDEA创建springboot项目遇到的问题Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found
    JDBC连接mysql出现The server time zone value '�й���׼ʱ��' is unrecognized
    Java反射、注解
    Junit单元测试
    Java-线程池、匿名内部类、Lambda表达式
  • 原文地址:https://www.cnblogs.com/kwinwei/p/13159408.html
Copyright © 2011-2022 走看看