乐趣区

关于c++:c-使用-rapidjson-生成-json-并解析

概述

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

罕用函数

AddMember

  • 性能:往对象中增加一个键值对。
  • 函数原型
GenericValue& rapidjson::GenericValue< Encoding, Allocator >::AddMember(    
    GenericValue< Encoding, Allocator > &     name,
    GenericValue< Encoding, Allocator > &     value,
    Allocator & allocator 
)    
  • 函数参数

    • name 是键的名称
    • value 是对应键的值
    • allocator 用来分配内存的分配器,必须与后面应用的雷同。

SetString

  • 性能:将此值设置为字符串,且复制原字符串。
  • 函数原型
GenericValue& rapidjson::GenericValue< Encoding, Allocator >::SetString(
const Ch *     s,
SizeType     length,
Allocator &     allocator 
)            
  • 函数参数

    • s 是原字符串
    • length 是原字符串的长度,包含前面的空字符
    • 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"
    }
  ]
}

参考资料

  • RapidJSON 文档
  • rapidjson 库的根本应用
退出移动版