Resources for reading

https://rapidjson.org/md_doc_tutorial.html

Reading json from a String

//rapidjson needs:
#include "rapidjson/document.h"
using namespace rapidjson;

	//Open json for reading
	Document JsonDocument;
	Value::ConstMemberIterator JsonField;
	Value::ConstMemberIterator JsonFieldNested1;
	string JsonFieldName;
	string JsonFieldString;
	int JsonFieldInt;

	if (!JsonDocument.Parse(MyJsonString.c_str()).HasParseError())
	{

		//Getting a string
		JsonField = JsonDocument.FindMember("MyFieldName1");
		if ( (JsonField != JsonDocument.MemberEnd()) && (JsonField->value.IsString()) )
			JsonFieldString = JsonField->value.GetString();


		//Getting an int
		JsonField = JsonDocument.FindMember("MyFieldName2");
		if ( (JsonField != JsonDocument.MemberEnd()) && (JsonField->value.IsInt()) )
			JsonFieldInt = JsonField->value.GetInt();


		//Getting a nested field (you can't read nested fields as a new string, they are an object)
		JsonField = JsonDocument.FindMember("MyFieldName3");
		if ( (JsonField != JsonDocument.MemberEnd()) && (JsonField->value.IsObject()) )
		{
			JsonFieldNested1 = JsonField->value.FindMember("MySubFieldName1");
			if ( (JsonFieldNested1 != JsonDocument.MemberEnd()) && (JsonFieldNested1->value.IsString()) )
			{
				MyString = JsonFieldNested1->value.GetString();
			}
		}

		//Iterating through nested fields (e.g. "MyFieldWithSubFields": { "MySubfield1": "abc", "MySubfield2": "24" }
		JsonField = JsonDocument.FindMember("MyFieldWithSubFields");
		if ( (JsonField != JsonDocument.MemberEnd()) && (JsonField->value.IsObject()) )		//Structures in a json doc are not a string in rapidjson and can't be read as a string, they are an object
		{
			//ITERATE THROUGH EACH MEMBER
			for (Value::ConstMemberIterator JsonIteratorMember = JsonField->value.MemberBegin(); JsonIteratorMember != JsonField->value.MemberEnd(); ++JsonIteratorMember)
			{
				//Get key name
				JsonFieldName = JsonIteratorMember->name.GetString();

				//Get value
				if (JsonIteratorMember->value.IsNumber())
				{
					JsonFieldInt = JsonIteratorMember->value.GetInt();
				}
				else if (JsonIteratorMember->value.IsString())
				{
					JsonFieldString = JsonIteratorMember->value.GetString();
				}
				else if (JsonIteratorMember->value.IsBool())
				{
					JsonFieldInt = (int)JsonIteratorMember->value.GetBool();
				}
				else if (JsonIteratorMember->value.IsArray())
				{
					for (SizeType Index = 0; Index < JsonIteratorMember->value.Size(); Index++)
					{
						if (JsonIteratorMember->value[Index].IsNumber())
						{
							JsonFieldInt = JsonIteratorMember->value[Index].GetInt() ;
						}
						else if (JsonIteratorMember->value[Index].IsString())
						{
							JsonFieldString = JsonIteratorMember->value[Index].GetString();
						}
						else if (JsonIteratorMember->value[Index].IsBool())
						{
							JsonFieldInt = (int)JsonIteratorMember->value[Index].GetBool();
						}
					}
				}

			} //for (Value::ConstMemberIterator JsonIteratorMember = JsonField->value.MemberBegin(); JsonIteratorMember != JsonField->value.MemberEnd(); ++JsonIteratorMember)
		}


	} //if (!JsonDocument.Parse(MyJsonString.c_str()).HasParseError())

Get json value functions

Calling
	JsonField = JsonDocument.FindMember("MyFieldName1");
	if (JsonField != JsonDocument.MemberEnd())
		JsonFieldString = JsonFieldGetValueAsString(JsonField);
The functions

//****************************************************
//****************************************************
//********** JSON FIELD GET VALUE AS STRING **********
//****************************************************
//****************************************************
//We always return a string regardless of json value type
string JsonFieldGetValueAsString (Value::ConstMemberIterator JsonField)
{
	stringstream ss1;
	
	if (JsonField->value.IsInt())
		ss1 << JsonField->value.GetInt();
	else if (JsonField->value.IsInt64())
		ss1 << JsonField->value.GetInt64();
	else if (JsonField->value.IsDouble())
		ss1 << JsonField->value.GetDouble();
	else if (JsonField->value.IsUint())
		ss1 << JsonField->value.GetUint();
	else if (JsonField->value.IsUint64())
		ss1 << JsonField->value.GetUint64();
	else if (JsonField->value.IsNumber())
		ss1 << JsonField->value.GetInt();
	else if (JsonField->value.IsBool())
		ss1 << (int)(JsonField->value.GetBool() ? 1 : 0);
	else if (JsonField->value.IsString())
		ss1 << JsonField->value.GetString();
	else
		ss1 << "";
	
	return (ss1.str());
}


//*************************************************
//*************************************************
//********** JSON FIELD GET VALUE AS INT **********
//*************************************************
//*************************************************
//We always return an int64 regardless of json value type
//Defaults to 0
int64_t JsonFieldGetValueAsInt64 (Value::ConstMemberIterator JsonField)
{
	stringstream ss1;
	int64_t Value;
	
	if (JsonField->value.IsInt())
		Value = JsonField->value.GetInt();
	else if (JsonField->value.IsInt64())
		Value = JsonField->value.GetInt64();
	else if (JsonField->value.IsDouble())
		Value = rint(JsonField->value.GetDouble());
	else if (JsonField->value.IsUint())
		Value = JsonField->value.GetUint();
	else if (JsonField->value.IsUint64())
		Value = JsonField->value.GetUint64();
	else if (JsonField->value.IsNumber())
		Value = JsonField->value.GetInt();
	else if (JsonField->value.IsBool())
		Value = (uint64_t)(JsonField->value.GetBool() ? 1 : 0);
	else if (JsonField->value.IsString())
	{
		ss1 << JsonField->value.GetString();
		ss1 >> Value;
	}
	else
		Value = 0;
	
	return(Value);
}


//****************************************************
//****************************************************
//********** JSON FIELD GET VALUE AS DOUBLE **********
//****************************************************
//****************************************************
//We always return an double regardless of json value type
//Defaults to 0
double JsonFieldGetValueAsDouble (Value::ConstMemberIterator JsonField)
{
	stringstream ss1;
	double Value;
	
	if (JsonField->value.IsInt())
		Value = (double)JsonField->value.GetInt();
	else if (JsonField->value.IsInt64())
		Value = (double)JsonField->value.GetInt64();
	else if (JsonField->value.IsDouble())
		Value = JsonField->value.GetDouble();
	else if (JsonField->value.IsUint())
		Value = (double)JsonField->value.GetUint();
	else if (JsonField->value.IsUint64())
		Value = (double)JsonField->value.GetUint64();
	else if (JsonField->value.IsNumber())
		Value = (double)JsonField->value.GetInt();
	else if (JsonField->value.IsBool())
		Value = (double)(JsonField->value.GetBool() ? 1 : 0);
	else if (JsonField->value.IsString())
	{
		ss1 << JsonField->value.GetString();
		ss1 >> Value;
	}
	else
		Value = 0;
	
	return(Value);
}

Read json from a file

#include "rapidjson/filereadstream.h"
#include <cstdio>
using namespace rapidjson;

	FILE* MyFile = fopen(FilePath.str().c_str(), "r");
	char ReadBuffer[65536];
	FileReadStream MyJsonReadStream(MyFile, ReadBuffer, sizeof(ReadBuffer));
	if(MyFile != NULL)
		fclose(MyFile);
	if (!JsonDocument.ParseStream(MyJsonReadStream).HasParseError())
	{
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *