C++ parses json format text and transplants third-party library jsoncpp [Lack of source code analysis]

One;'Transplant the third-party library
Download the third-party library source website
https://sourceforge.net/projects/jsoncpp/

Compile the static lib of the third-party library
https://www.cnblogs.com/arxive/p/11220854.html

Note that the link should be consistent with the format of the generation. Here are all multi-threaded MDT links
https://blog.csdn.net/chenxun_2010/article/details/41847131

When compiling, also pay attention to the use of "" when including include, which is easy to edit.

Two; use the third-party library API
1. parse the json file

解析的json文件
{
    
    
    "name": "BeJson",
    "url": "http://www.bejson.com",
    "page": 88,
    "isNonProfit": true,
    "address": {
    
    
        "C1":{
    
    
			"street": "科技园路.",
			"IP":"123"
		},
		"C2":{
    
    
			"street": "江苏苏州.",
			"IP":"1234"
		},
		"C3":{
    
    
			"street": "中国.",
			"IP":"12345"
		}
    },
    "links": [
        {
    
    
            "name": "Google",
            "url": "http://www.google.com"
        },
        {
    
    
            "name": "Baidu",
            "url": "http://www.baidu.com"
        },
        {
    
    
            "name": "SoSo",
            "url": "http://www.SoSo.com"
        }
    ]
}
解析核心代码
Json::Reader reader;
   Json::Value root;

   int nRole = 0;
   string strname;
   string strurl;
   int ipage;
   bool bisNonProfit;
   vector<string> vc_strMemerName;

   // 解析json内容
   if (reader.parse(is, root))
   {
    
    
	   strname = root["name"].asString();
	   strurl = root["url"].asString();
	   ipage = root["page"].asInt();
	   bisNonProfit = root["isNonProfit"].asBool();
		
	   cout << "name:" << strname << endl;
	   cout << "url:" << strurl << endl;
	   cout << "page:" << ipage << endl;
	   cout << "isNonProfit:" << bisNonProfit << endl;
	   Json::Value Obj_address;
	   bool b_isAddressObj = root.isMember("address");//遍历对象组合
	   if (b_isAddressObj)
	   {
    
    
		   int i_size = root["address"].size();
		   vc_strMemerName = root["address"].getMemberNames();
		   for (int i = 0; i < vc_strMemerName.size(); i++)
		   {
    
    
			   cout << vc_strMemerName[i] << ":" << root["address"][vc_strMemerName[i]]["street"].asString() << endl;
			   cout << vc_strMemerName[i] << ":" << root["address"][vc_strMemerName[i]]["IP"].asString() << endl;
		   }
	   }
	   if (root["links"].isArray())//遍历数组是对象的组合
	   {
    
    
		   int i_size = root["links"].size();
		   for (int i = 0; i < i_size; i++)
		   {
    
    
			   cout << root["links"][i]["name"].asString() << endl;
			   cout << root["links"][i]["url"].asString() << endl;
		   }
	   }
   }

   is.close();

2. Recursively parse the menu library and output the menu hierarchy.
The format is that some object nodes contain children array nodes (which contain multiple objects, and the objects can contain children arrays)

大致为很多children这种类型,提取出children子菜单的菜单层级表
{
    
    
	"data"{
    
    
		"jsonMenu"{
    
    
			"id":"123",
			"displayName":"zw""Type""P",
			 "children"[
			 	{
    
    
			 		"id":"1233",
					"displayName":"z2w""Type""P1",
					 "children":
					 [
					 ]
				},
				{
    
    
			 		"id":"12335",
					"displayName":"z2w2""Type""P13",
					 "children":
					 [
						{
    
    
							"id":"12336",
							"displayName":"z2w42""Type""P13234",
							 "children":
							 [
								
							 ]
						},
						{
    
    
							"id":"1233ss6",
							"displayName":"z2w4sd2""Type""P13qwew234",
							 "children":
							 [
								"id":"1s6",
								"displayName":"w4sd2""Type""P134",
								 "children":
								 [
									
								 ]
							 ]
						}
					 ]
				}
			 ]
		}
	}
}

Insert picture description here
Three; jsonCpp source code learning (if the json file array is parsed and stored in the root to let us extract it)
, after reading the third part of the C++ course, fill in it in time, and parse the XML format file and its source code

Guess you like

Origin blog.csdn.net/zw1996/article/details/108249996