• c++ rapidjson读取json文件 解析


    库:链接:https://pan.baidu.com/s/1UChrgqLPJxKopyqShDCHjg  密码:3yhz

    #include <iostream>
    #include <string>
    #include "rapidjson/document.h"
    #include "rapidjson/writer.h"
    #include "rapidjson/stringbuffer.h"
    
    using namespace rapidjson;
    using namespace std;
    
    string readfile(const char *filename){
        FILE *fp = fopen(filename, "rb");
        if(!fp){
            printf("open failed! file: %s", filename);
            return "";
        }
        
        char *buf = new char[1024*16];
        int n = fread(buf, 1, 1024*16, fp);
        fclose(fp);
        
        string result;
        if(n>=0){
            result.append(buf, 0, n);
        }
        delete []buf;
        return result;
    }
    
    int parseJSON(const char *jsonstr){
        Document d;
        if(d.Parse(jsonstr).HasParseError()){
            printf("parse error!
    ");
            return -1;
        }
        if(!d.IsObject()){
            printf("should be an object!
    ");
            return -1;
        }
        if(d.HasMember("errorCode")){
            Value &m = d["errorCode"];
            int v = m.GetInt();
            printf("errorCode: %d
    ", v);
        }
        printf("show numbers: 
    ");
        if(d.HasMember("numbers")){
            Value &m = d["numbers"];
            if(m.IsArray()){
                for(int i = 0; i < m.Size(); i++){
                    Value &e = m[i];
                    int n = e.GetInt();
                    printf("%d,", n);
                }
            }
        }
        return 0;
    }
    
    int parseJSON2(const char *jsonstr){
        Document d;
        if(d.Parse(jsonstr).HasParseError()){
            throw string("parse error!
    ");
        }
        if(!d.IsObject()){
            throw string("should be an object!
    ");
        }
        if(!d.HasMember("errorCode")){
            throw string("'errorCode' no found!");
        }
        
        Value &m = d["errorCode"];
        int v = m.GetInt();
        printf("errorCode: %d
    ", v);
        printf("show numbers:
    ");
        if(d.HasMember("numbers")){
            Value &m = d["numbers"];
            if(m.IsArray()){
                for(int i = 0; i < m.Size(); i++){
                    Value &e = m[i];
                    int n = e.GetInt();
                    printf("%d", n);
                }
            }
        }
        return 0;
    }
    
    /*
     //path="/Users/macname/Desktop/example.json"
     
     {
     "errorCode":0,
     "reason":"OK",
     "result":{"userId":10086,"name":"中国移动"},
     "numbers":[110,120,119,911]
     }
     
     */
    int main(){
        
        string jsonstr = readfile("/Users/macname/Desktop/example.json");
        //parseJSON(jsonstr.c_str());
        
        try{
            parseJSON2(jsonstr.c_str());
        }catch(string e){
            printf("error: %s 
    ", e.c_str());
        }
        getchar();
        return 0;
    }

    输出

    errorCode: 0
    show numbers:
    110120119911

  • 相关阅读:
    .Net中集合排序还可以这么玩
    C# 6.0中你不知道的新特性
    EF Core利用Transaction对数据进行回滚保护
    dot watch+vs code提升asp.net core开发效率
    .Net小白的大学四年,内含面经
    EF Core利用Scaffold从根据数据库生成代码
    利用EF Core的Join进行多表查询
    EF Core下利用Mysql进行数据存储在并发访问下的数据同步问题
    新建.Net Core应用程序后引用项一直黄色感叹号怎么办?
    用户密码传输和存储的保护
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11105387.html
Copyright © 2020-2023  润新知