Specific steps:
-
First download the dependency package of rapidjson
Method 1: Use git directly to download
Address: git clone https://github.com/miloyip/rapidjson.git
Method 2: Download the dependency package I uploaded
-
Introduce dependent packages into the project
1 Put the decompressed file in your c++ project
2 Import the src directory in the rapidjson file into the project
step:
1 Open the project, project properties
2 Click properties ----- "c++------" add src directory -- "confirm and exit
At this point, your project can use rapidjson
-
Start using rapidjson (there are some caveats)
create header file
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/istreamwrapper.h"
#include "rapidjson/ostreamwrapper.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace rapidjson;
using namespace std;
int rapidJsonTest();
create cpp file
#include "rapidJsonTest.h"
#include <vector>
#include "memory.h"
#include<ctime>
#include<stdio.h>int rapidJsonTest()
{Document jsonDoc;
Document::AllocatorType& allocator = jsonDoc.GetAllocator();
jsonDoc.SetObject();//Main object
Value value(kObjectType);
value.AddMember("flag", 0, allocator);
value.AddMember("msg", "Operation succeeded", allocator);
//result object
Value result(kObjectType);
//Add basic information of result
result.AddMember("name", "(anonymous)", allocator);
result.AddMember("right_query", true, allocator);//Create an array object to store t information
Value arraych(kArrayType);
struct channels
{ int channel; string username; string telphone; };
for (int i = 0; i < 5; i++) {
chunnels ch;
ch.chunnel = 0;
ch.username = "黎明";
ch.telphone = "110";Value chu(kObjectType);
Value v0;
v0.SetInt(ch.chunnel); //The numerical type is written like this
Value v1(ch.username.c_str(), allocator); //If the string is written like this, there will be no problem with /u0000
Value v2(ch.telphone.c_str(), allocator);chu.AddMember("chunnel", v0, allocator);
chu.AddMember("username", v1, allocator);
chu.AddMember("telphone", v2, allocator);chu.AddMember("The third way of writing", "6666", allocator); //If the content is direct, there is no need to transfer
arraych.PushBack(chu, allocator);
}
//Add the collection to the result object
result.AddMember("chunnels", arraych, allocator);
//Add the result object to the main object
value.AddMember("result", result, allocator);// 转为string
StringBuffer str;
Writer<StringBuffer> writer(str);
value.Accept(writer);
string strJson = str.GetString();
cout << strJson << endl;return 0;
}
case two
int demo2(){
cout << "New creation of json Value and access to key" << endl;
Document jsonDoc; //Generate a dom element Document
Document::AllocatorType& allocator = jsonDoc.GetAllocator(); //Get the allocator
jsonDoc.SetObject(); //Set the current Document as an object, that is, the entire Document Is a dom element of type Object// Create a new Value object 1 (object type)
Value value1(kObjectType);
value1.AddMember("name", "language", allocator); // string type (assign a value to the field, the key must be string type)
value1. AddMember("score", 80, allocator); // integer type
value1.AddMember("right", true, allocator); // integer type
value1.AddMember("percent", 12.3456789123, allocator);// double type// It is possible to access the key at this time
if (value1.HasMember("name")) // Determine whether the key exists
{ if (value1["name"].IsString()) // Then determine whether the type is correct { cout << "value1:name:" << value1["name"].GetString() << endl; } cout << "value1:score:" << value1["score"].GetInt() << endl; // It is risky to write directly like this }
// Create a new Value object (array type)
Value value2(kArrayType);
value2.PushBack(1, allocator);
value2.PushBack(2, allocator);
value2.PushBack(3, allocator);
cout << "value:size( ) Number of elements in the array: " << value2.Size() << endl;// Merge a whole
Value value3(kObjectType);
value3.AddMember("name", "xiaoming", allocator);
value3.AddMember("age", 18, allocator);
value3.AddMember("value1", value1, allocator ); // the entire value1 as the value of the key
value3.AddMember("value2", value2, allocator); // the entire value2 as the value of the key// 转为string
StringBuffer str;
Writer<StringBuffer> writer(str);
value3.Accept(writer);
string strJson = str.GetString();
cout << "value3:" << strJson.c_str() << endl;}
Remember that when using rapidjson, for the string type and numeric type, you need to use the form of rapidjson to encapsulate, otherwise there will be errors