欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++__RapidJson

程序员文章站 2024-02-04 08:26:34
...
//例1:Json解析
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
using namespace rapidjson;

{
    ...    
    std::ifstream t("D:\\debug\\debug_2020_12_18_eqs_example\\eqs_example\\eqs_parser\\eqs_param.json");
    std::string strJosn((std::istreambuf_iterator<char>(t)), 
                    std::istreambuf_iterator<char>());
    rapidjson::Document doc;
    doc.Parse(strJosn.c_str());
    cout << "---------------------------eqs_param.json----------------------------" << endl;
    cout << strJosn << endl;
    cout << "---------------------------------------------------------------------" << endl;
    
    ERGEBNIS ergebnis;
    if (doc.HasMember("ERGEBNIS"))
    {
        if (doc.HasMember("ERGEBNIS") && doc["ERGEBNIS"].IsObject())
        {
            const rapidjson::Value& ergebnisObj = doc["ERGEBNIS"];
            if (ergebnisObj.HasMember("name") && ergebnisObj["name"].IsString())
            {
                ergebnis.name = ergebnisObj["name"].GetString();
            }
            if (ergebnisObj.HasMember("res_ver") && ergebnisObj["res_ver"].IsString())
            {
                ergebnis.res_ver = ergebnisObj["res_ver"].GetString();
            }
            if (ergebnisObj.HasMember("nlength") && ergebnisObj["nlength"].IsInt())
            {
                ergebnis.nlength = ergebnisObj["nlength"].GetInt();
            }
            if (ergebnisObj.HasMember("nResultNum") && ergebnisObj["nResultNum"].IsInt())
            {
                ergebnis.nResultNum = ergebnisObj["nResultNum"].GetInt();
            }
            if (ergebnisObj.HasMember("exist_result") && ergebnisObj["exist_result"].IsBool())
            {
                ergebnis.exist_result = ergebnisObj["exist_result"].GetBool();
            }

            if (ergebnisObj.HasMember("Result") && ergebnisObj["Result"].IsArray())
            {
                const rapidjson::Value& array = ergebnisObj["Result"];
                size_t len = array.Size();
                for (size_t i = 0; i < len; i++)
                {
                    const rapidjson::Value& resultObj = array[i];
                    RESULT reslut;
                    if (resultObj.IsObject())
                    {
                        if (resultObj.HasMember("name") && resultObj["name"].IsString())
                        {
                            reslut.name = resultObj["name"].GetString();
                        }
                        if (resultObj.HasMember("resultID") && resultObj["resultID"].IsString())
                        {
                            reslut.resultID = resultObj["resultID"].GetString();
                        }
                        if (resultObj.HasMember("system_type") && resultObj["system_type"].IsString())
                        {
                            reslut.system_type = resultObj["system_type"].GetString();
                        }
                        if (resultObj.HasMember("ver") && resultObj["ver"].IsString())
                        {
                            reslut.ver = resultObj["ver"].GetString();
                        }
                        if (resultObj.HasMember("ergeb") && resultObj["ergeb"].IsString())
                        {
                            reslut.ergeb = resultObj["ergeb"].GetString();
                        }
                        if (resultObj.HasMember("timestamp") && resultObj["timestamp"].IsString())
                        {
                            reslut.timestamp = resultObj["timestamp"].GetString();
                        }
                        if (resultObj.HasMember("mode") && resultObj["mode"].IsString())
                        {
                            reslut.mode = resultObj["mode"].GetString();
                        }
                        if (resultObj.HasMember("exist_link") && resultObj["exist_link"].IsBool())
                        {
                            reslut.exist_link = resultObj["exist_link"].GetBool();
                        }
                        if (resultObj.HasMember("link") && resultObj["link"].IsObject())
                        {
                            const rapidjson::Value& linkObj = resultObj["link"];
                            if (linkObj.HasMember("name") && linkObj["name"].IsString())
                            {
                                reslut.link.name = linkObj["name"].GetString();
                            }
                            if (linkObj.HasMember("text") && linkObj["text"].IsString())
                            {
                                reslut.link.text = linkObj["text"].GetString();
                            }
                        }
                        if (resultObj.HasMember("exist_ergtxt") && resultObj["exist_ergtxt"].IsBool())
                        {
                            reslut.exist_ergtxt = resultObj["exist_ergtxt"].GetBool();
                        }
                        if (resultObj.HasMember("ergtxt") && resultObj["ergtxt"].IsObject())
                        {
                            const rapidjson::Value& ergtxtObj = resultObj["ergtxt"];
                            if (ergtxtObj.HasMember("name") && ergtxtObj["name"].IsString())
                            {
                                reslut.ergtxt.name = ergtxtObj["name"].GetString();
                            }
                            if (ergtxtObj.HasMember("nlength") && ergtxtObj["nlength"].IsInt())
                            {
                                reslut.ergtxt.nlength = ergtxtObj["nlength"].GetInt();
                            }
                            if (ergtxtObj.HasMember("text") && ergtxtObj["text"].IsString())
                            {
                                reslut.ergtxt.text = ergtxtObj["text"].GetString();
                            }
                        }
                        if (resultObj.HasMember("exist_value") && resultObj["exist_value"].IsBool())
                        {
                            reslut.exist_value = resultObj["exist_value"].GetBool();
                        }
                        if (resultObj.HasMember("value_num") && resultObj["value_num"].IsInt())
                        {
                            reslut.value.nValueContent_num = resultObj["value_num"].GetInt();
                        }
                        if (resultObj.HasMember("value") && resultObj["value"].IsArray())
                        {
                            const rapidjson::Value& array = resultObj["value"];
                            size_t len = array.Size();
                            for (size_t i = 0; i < len; i++)
                            {
                                const rapidjson::Value& valueObj = array[i];
                                VALUE_CONTENT value_content;
                                if (valueObj.IsObject())
                                {
                                    if (valueObj.HasMember("id") && valueObj["id"].IsInt())
                                    {
                                        value_content.id = valueObj["id"].GetInt();
                                    }
                                    if (valueObj.HasMember("value") && valueObj["value"].IsString())
                                    {
                                        value_content.value = valueObj["value"].GetString();
                                    }
                                    if (valueObj.HasMember("uint") && valueObj["uint"].IsString())
                                    {
                                        value_content.uint = valueObj["uint"].GetString();
                                    }
                                    if (valueObj.HasMember("ok") && valueObj["ok"].IsString())
                                    {
                                        value_content.ok = valueObj["ok"].GetString();
                                    }
                                    if (valueObj.HasMember("exist_mwtxt") && valueObj["exist_mwtxt"].IsBool())
                                    {
                                        value_content.exist_mwtxt = valueObj["exist_mwtxt"].GetBool();
                                    }
                                    if (valueObj.HasMember("mwtxt") && valueObj["mwtxt"].IsObject())
                                    {
                                        const rapidjson::Value& mwtxtObj = valueObj["mwtxt"];
                                        if (mwtxtObj.HasMember("name") && mwtxtObj["name"].IsString())
                                        {
                                            value_content.mwtxt.name = mwtxtObj["name"].GetString();
                                        }
                                        if (mwtxtObj.HasMember("nlength") && mwtxtObj["nlength"].IsInt())
                                        {
                                            value_content.mwtxt.nlength = mwtxtObj["nlength"].GetInt();
                                        }
                                        if (mwtxtObj.HasMember("text") && mwtxtObj["text"].IsString())
                                        {
                                            value_content.mwtxt.text = mwtxtObj["text"].GetString();
                                        }
                                    }
                                }
                                reslut.value.value_content.push_back(value_content);
                            }
                        }
                    }
                    ergebnis.vResult.push_back(reslut);
                }
            }
        }
    }
    ...
}








//解析的是这个Json报文
{
    "ERGEBNIS": {
        "name": "ERGEBNIS",
        "res_ver": "002",
        "nlength": 648,
        "nResultNum": 2,
        "exist_result": true,
        "Result": [
            {
                "name": "RESULT",
                "resultID": "VW10SCHRAUBER4567890",
                "system_type": "ANLAGEXXXX",
                "ver": "VERSXX",
                "ergeb": "NIO_",
                "timestamp": "20021020121200",
                "mode": "Auto",
                "exist_link": false,
                "link": {
                    "name":"LINK",
                    "text": ""
                },
                "exist_ergtxt": true,
                "ergtxt": {
                    "name": "ERGTXT",
                    "nlength": 75,
                    "text": "text for the result may be placed here; Attention, please observe the notes!"
                },
                "exist_value": true,
                "value_num": 3,
                "value": [
                    {
                        "id": 1,
                        "value": "431.20",
                        "uint": "kg",
                        "ok": "0",
                        "exist_mwtxt": true,
                        "mwtxt": {
                            "name": "MWTXT",
                            "nlength": 46,
                            "text": "text for the measured value may be placed here"
                        }
                    },
                    {
                        "id": 2,
                        "value": "431.20",
                        "uint": "kg",
                        "ok": "0",
                        "exist_mwtxt": true,
                        "mwtxt": {
                            "name": "MWTXT",
                            "nlength": 46,
                            "text": "text for the measured value may be placed here"
                        }
                    },
                    {
                        "id": 3,
                        "value": "431.20",
                        "uint": "kg",
                        "ok": "0",
                        "exist_mwtxt": true,
                        "mwtxt": {
                            "name": "MWTXT",
                            "nlength": 46,
                            "text": "text for the measured value may be placed here"
                        }
                    }
                ]
            },
            {
                "name": "RESULT",
                "resultID": "VW10SCHRAUBER4567890",
                "system_type": "ANLAGEXXXX",
                "ver": "VERSXX",
                "ergeb": "NIO_",
                "timestamp": "20021020121200",
                "mode": "Auto",
                "exist_link": false,
                "link": "",
                "exist_ergtxt": false,
                "exist_value": false
            }
        ]
    }
}
//例:生成Json报文
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
using namespace rapidjson;

{
    Document doc; //生成DOM元素
    doc.SetObject();
    Document::AllocatorType& allocator = doc.GetAllocator(); //生成一个分配器

    //构建键值对
    doc.AddMember("name", "jack", allocator);
    doc.AddMember("age", 18, allocator);

    //构建数组元素 ["1", "2"]
    Value array_sub(kArrayType);
    array_sub.PushBack("a", allocator);
    array_sub.PushBack("b", allocator);
    doc.AddMember("sub", array_sub, allocator);

    //构建数组object
    rapidjson::Value array_json(rapidjson::kArrayType);
    rapidjson::Value obj(rapidjson::kObjectType);//每一个数组里面是一个json格式
    obj.AddMember("a", "A", allocator);
    obj.AddMember("b", "B", allocator);
    array_json.PushBack(obj, allocator);

    rapidjson::Value obj1(rapidjson::kObjectType);//每一个数组里面是一个json格式
    obj1.AddMember("c", "C", allocator);
    obj1.AddMember("d", "D", allocator);
    array_json.PushBack(obj1, allocator);
    doc.AddMember("elp", array_json, allocator);

    rapidjson::StringBuffer s;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
    doc.Accept(writer);

    string str = std::string(s.GetString());

    std::string outFileName = "json11111.json";
    std::ofstream outfile(outFileName, std::ios::trunc);
    outfile << s.GetString();
    outfile.close();
}

//注生成的是这个Json
{
    "name": "jack",
    "age": 18,
    "sub": [
        "a",
        "b"
    ],
    "elp": [
        {
            "a": "A",
            "b": "B"
        },
        {
            "c": "C",
            "d": "D"
        }
    ]
}

 

相关标签: 工具

推荐阅读