c++ 使用 rapidjson 生成 json 并解析

2019-10-08  本文已影响0人  云胡同学

概述

使用 rapidjson 生成 json 并对其进行解析。

常用函数

AddMember

GenericValue& rapidjson::GenericValue< Encoding, Allocator >::AddMember(    
    GenericValue< Encoding, Allocator > &   name,
    GenericValue< Encoding, Allocator > &   value,
    Allocator & allocator 
)   

SetString

GenericValue& rapidjson::GenericValue< Encoding, Allocator >::SetString(
  const Ch *    s,
  SizeType  length,
  Allocator &   allocator 
)           

代码

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include "rapidjson/prettywriter.h"
#include "rapidjson/document.h"
#include "rapidjson/istreamwrapper.h"

using namespace std;
using namespace rapidjson;

//-----------------------------------------------------------------------------
void AddBasicType(Document &d, Document::AllocatorType & allocator)
{
    d.AddMember("digit", 3, allocator);           // 添加整型数据
    d.AddMember("height", 170.5, allocator);      // 添加浮点类型
    d.AddMember("subject1", "math", allocator);   // 添加字符串类型,常量方式

    string strSubject = "English";
    Value valueSubject(kStringType);
    valueSubject.SetString(strSubject.c_str(), strSubject.size(), allocator);

    if (!valueSubject.IsNull())
    {
        d.AddMember("subject2", valueSubject, allocator); // 添加字符串类型,变量方式,不能直接对变量进行添加,类型不对
    }
}

//-----------------------------------------------------------------------------
void AddObjectType(Document &d, Document::AllocatorType & allocator)
{
    Value valueObject(kObjectType);
    valueObject.AddMember("sex", "male", allocator);
    valueObject.AddMember("weight", 95, allocator);
    d.AddMember("info", valueObject, allocator);
}

//-----------------------------------------------------------------------------
void AddStringArrayType(Document &d, Document::AllocatorType & allocator)
{
    Value valueStr(kStringType);      // 单个字符串
    Value valueStrArray(kArrayType);  // 字符串数组

    string strExtraSubject1 = "计算机";
    string strExtraSubject2 = "音乐";
    string strExtraSubject3 = "体育";

    valueStr.SetString(strExtraSubject1.c_str(), strExtraSubject1.size(), allocator);
    valueStrArray.PushBack(valueStr, allocator);

    valueStr.SetString(strExtraSubject2.c_str(), strExtraSubject2.size(), allocator);
    valueStrArray.PushBack(valueStr, allocator);

    valueStr.SetString(strExtraSubject3.c_str(), strExtraSubject3.size(), allocator);
    valueStrArray.PushBack(valueStr, allocator);

    d.AddMember("extraSubject", valueStrArray, allocator);
}

//-----------------------------------------------------------------------------
void AddObjectArrayType(Document &d, Document::AllocatorType & allocator)
{
    Value valueObjectArray(kArrayType);
    for (int i = 0; i < 3; i++)
    {
        Value valueSingleObject(kObjectType);
        valueSingleObject.AddMember("score", i * 40, allocator);
        if (i == 0)
        {
            valueSingleObject.AddMember("difficulty", "hard", allocator);
        }
        else if (i == 1)
        {
            valueSingleObject.AddMember("difficulty", "abnormal", allocator);
        }
        else 
        {
            valueSingleObject.AddMember("difficulty", "easy", allocator);
        }
        valueObjectArray.PushBack(valueSingleObject, allocator);
    }
    d.AddMember("subjectInfo", valueObjectArray, allocator);
}

//-----------------------------------------------------------------------------
void ParseBasicType(Document &d, string jsonData)
{
    if (!d.Parse(jsonData.data()).HasParseError())
    {
        // 解析整型
        if (d.HasMember("digit") && d["digit"].IsInt())
        {
            cout << "digit = " << d["digit"].GetInt() << endl;
        }

        // 解析浮点型
        if (d.HasMember("height") && d["height"].IsDouble())
        {
            cout << "height = " << d["height"].GetDouble() << endl;
        }

        // 解析字符串类型
        if (d.HasMember("subject1") && d["subject1"].IsString())
        {
            cout << "subject1 = " << d["subject1"].GetString() << endl;
        }

        if (d.HasMember("subject2") && d["subject1"].IsString())
        {
            cout << "subject2 = " << d["subject2"].GetString() << endl;
        }
    }
}

//-----------------------------------------------------------------------------
void ParseObjectType(Document &d, string jsonData)
{
    if (d.HasMember("info") && d["info"].IsObject())
    {
        const Value& resInfo = d["info"]; // 获取对象
        if (resInfo.HasMember("sex") && resInfo["sex"].IsString())
        {
            cout << "resInfo.sex = " << resInfo["sex"].GetString() << endl;
        }
        if (resInfo.HasMember("weight") && resInfo["weight"].IsInt())
        {
            cout << "resInfo.weight = " << resInfo["weight"].GetInt() << endl;
        }
    }
}

//-----------------------------------------------------------------------------
void ParseStringArrayType(Document &d, string jsonData)
{
    if (d.HasMember("extraSubject") && d["extraSubject"].IsArray())
    {
        const Value& valueStrArray = d["extraSubject"];
        size_t len = valueStrArray.Size(); // 获取数组长度

        for (size_t i = 0; i < len; i++)
        {
            cout << "extraSubject[" << i << "] = " << valueStrArray[i].GetString() << endl;
        }
    }
}

//-----------------------------------------------------------------------------
void ParseObjectArrayType(Document &d, string jsonData)
{
    if (d.HasMember("subjectInfo") && d["subjectInfo"].IsArray())
    {
        const Value& valueObjectArray = d["subjectInfo"]; // 获取对象数组
        size_t len = valueObjectArray.Size();
        for (size_t i = 0; i < len; i++)
        {
            const Value& object = valueObjectArray[i]; // 每一个元素都是一个对象

            if (object.IsObject())
            {
                cout << "subjectInfo[" << i << "]: ";
                if (object.HasMember("score") && object["score"].IsInt())
                {
                    cout << "score = " << object["score"].GetInt();
                }
                if (object.HasMember("difficulty") && object["difficulty"].IsString())
                {
                    cout << " difficulty = " << object["difficulty"].GetString();
                }
            }
            cout << endl;
        }
    }
}

//-----------------------------------------------------------------------------
int main()
{
    Document d;
    d.SetObject();
    wstring wsValue;
    Document::AllocatorType &allocator = d.GetAllocator(); // 获取分配器
    
    AddBasicType(d, allocator);         // 添加基本类型
    AddObjectType(d, allocator);        // 添加对象类型,对象中有基本数据类型
    AddStringArrayType(d, allocator);   // 添加字符串数组类型,数组中的每一个元素是一个字符串
    AddObjectArrayType(d, allocator);   // 添加对象数组类型,数组中的每一个元素是一个对象

    // StringBuffer是一个简单的输出流。它分配一个内存缓冲区,供写入整个JSON。
    // 可使用GetString()来获取该缓冲区。
    StringBuffer strBuf;
    Writer<StringBuffer> writer(strBuf);
    d.Accept(writer);

    string jsonData = strBuf.GetString();
    cout << jsonData << endl << endl;

    ParseBasicType(d, jsonData);        // 解析基本类型
    ParseObjectType(d, jsonData);       // 解析对象类型
    ParseStringArrayType(d, jsonData);  // 解析字符串数组类型
    ParseObjectArrayType(d, jsonData);  // 解析对象数组类型
}

生成的 json 数据

{
  "digit": 3,
  "height": 170.5,
  "subject1": "math",
  "subject2": "English",
  "info": {
    "sex": "male",
    "weight": 95
  },
  "extraSubject": [
    "计算机",
    "音乐",
    "体育"
  ],
  "subjectInfo": [
    {
      "score": 0,
      "difficulty": "hard"
    },
    {
      "score": 40,
      "difficulty": "abnormal"
    },
    {
      "score": 80,
      "difficulty": "easy"
    }
  ]
}

参考资料

上一篇下一篇

猜你喜欢

热点阅读