当前位置:网站首页>QT (36) -rapidjson parsing nested JSON

QT (36) -rapidjson parsing nested JSON

2022-06-25 00:59:00 Cloudy summer

The previous article wrote the simplest call . This article describes nested parsing .
value value : character string , Numbers , Array ,object, Nested arrays ,Bool
Generate json file :


// Generate a string of json Format string , And analyze 
//value value : character string , Numbers , Array ,object, Nested arrays ,Bool
// {
// "name":"test",   
// "age":20,
// "letter":["a","b","c"],
// "location": {"province":"fujian","city":"xiamen","number":16}
// "book":[{"name":"book1", "isbn":"123"},{"name":"book2","isbn":"456"}],
// "healthy":true,
// }


 

rapidjson::Document jsonDoc;                    //1. Generate a dom Elements Document
rapidjson::Document::AllocatorType &allocator = jsonDoc.GetAllocator(); 
jsonDoc.SetObject();                            //  Set up root dom by object type 
 

jsonDoc.AddMember("name", "test", allocator);    //2. Add child nodes    String type 
jsonDoc.AddMember("age", 20, allocator);       
 

rapidjson::Value letterArray(rapidjson::kArrayType);
letterArray.PushBack("a", allocator);
letterArray.PushBack("b", allocator);
letterArray.PushBack("c", allocator);
jsonDoc.AddMember("letter", letterArray, allocator);    //3. Add child nodes   Array type 
 

rapidjson::Value locationObj(rapidjson::kObjectType);
locationObj.AddMember("province", "fujian", allocator);
locationObj.AddMember("city", "xiamen", allocator);
locationObj.AddMember("number", 16, allocator);
jsonDoc.AddMember("location", locationObj, allocator);  //4. Add child nodes  object  nesting object
 

rapidjson::Value bookArray(rapidjson::kArrayType);
rapidjson::Value book1(rapidjson::kObjectType); 
book1.AddMember("name", "book1", allocator);
book1.AddMember("isbn", "123", allocator);
bookArray.PushBack(book1, allocator);                  
 
rapidjson::Value book2(rapidjson::kObjectType); 
book2.AddMember("name", "book2", allocator);
book2.AddMember("isbn", "456", allocator);
bookArray.PushBack(book2, allocator);           
jsonDoc.AddMember("book", bookArray, allocator);     //5. Add child nodes   Array nesting object 
 

jsonDoc.AddMember("healthy", true, allocator);       //5. Add child nodes    add to bool Type values 
// jsonDoc.AddMember("sports", NULL, allocator);
 

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
jsonDoc.Accept(writer);
 
std::string strJson = std::string( buffer.GetString() );
printf("----- Generated Json:\n%s", strJson.c_str());
 

std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "w");          Write to file windows Platform to use wb
if (myFile) 
{
	fputs(buffer.GetString(), myFile);
	fclose(myFile);

}

analysis json Format :

 

rapidjson::Document newDoc;
std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "r");   //windows Platform use rb
if (myFile) 
{
	char buffer[256];
	FileReadStream inputStream(myFile,buffer,sizeof(buffer));  //1. Create an input stream 
	newDoc.ParseStream<0>(inputStream);                       //2. Convert read content to dom Elements 
	fclose(myFile);                                            //3. Close file , Very important 
}

if (newDoc.HasParseError()) 
{
	printf("Json Parse error:%d\n", newDoc.GetParseError()); 
}
else 
{
	//1. analysis  "name":"test"
	if (newDoc.HasMember("name")) 
	{
		printf("name:%s\n", newDoc["name"].GetString()); 
	}
	else 
	{}
     
    //2. analysis  "age":20
	if (newDoc.HasMember("age")) 
	{
		printf("age:%d\n", newDoc["age"].GetInt());  
	}
	else 
	{}
    //3. analysis  array  Array  "letter":["a","b","c"],
	if (newDoc.HasMember("letter")) 
	{
		rapidjson::Value letter;    
		letter = newDoc["letter"];
 		if (letter.IsArray() && !letter.Empty()) 
		{
			for (rapidjson::SizeType i = 0; i < letter.Size(); i++) 
			{
				printf("letter:%s\n", letter[i].GetString());
			}
		}
		else 
		{}
	}
	else {}
    //3. analysis  object {"province":"fujian","city":"xiamen","number":16}
	if (newDoc.HasMember("location")) 
	{
		rapidjson::Value location;      
		location = newDoc["location"];
 		if (location.IsObject()) 
		{
 			if (location.HasMember("province")) 
			{
				printf("location:province:%s\n", location["province"].GetString());
			}
			else {}
			if (location.HasMember("city")) 
			{
				printf("location:city:%s\n", location["city"].GetString());
			}
			else {}
			if (location.HasMember("number")) 
			{
				printf("location:number:%d\n", location["number"].GetInt());
			}
			else {}
		}
		else {}
	}
	else {}
     //4. analysis   Array nesting object [{"name":"book1", "isbn":"123"},{"name":"book2","isbn":"456"}]
	if (newDoc.HasMember("book")) 
	{
		rapidjson::Value book;
		book = newDoc["book"];
 
		// First Array
		if (book.IsArray() && !book.Empty()) 
		{
			rapidjson::Value tempBook;
			for (rapidjson::SizeType i = 0; i < book.Size(); i++) 
			{
				tempBook = book[i];  
				if (tempBook.IsObject()) 
				{
 
					if (tempBook.HasMember("name") && tempBook.HasMember("isbn")) 
					{
						printf("book:%d:name:%s, isbn:%s\n", i, tempBook["name"].GetString(), tempBook["isbn"].GetString());
					}
					else {}
				}
				else {}
			}
		}
		else {}
	}
	else {}
 
	if (newDoc.HasMember("healthy")) 
	{
		if (newDoc["healthy"].GetBool()) 
		{
			printf("healthy:true\n");
		}
		else 
		{
			printf("healthy:false\n");
		}
	}
	else {}
}



 

原网站

版权声明
本文为[Cloudy summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242006274226.html