C++解析json格式文本移植第三方库jsoncpp[缺源码解析]

一;’移植第三方库
下载第三方库源码网站
https://sourceforge.net/projects/jsoncpp/

编译第三方库的静态lib
https://www.cnblogs.com/arxive/p/11220854.html

注意链接时要与生成是的格式形式一致这里都是多线程的MDT链接
https://blog.csdn.net/chenxun_2010/article/details/41847131

编译的时候还注意包含include的时候采用""容易编过,

二;使用第三方库API
1、解析json文件

解析的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、递归解析菜单库,输出菜单层级
其格式为有部分对象节点中包含children数组节点(其中包含多个对象,而对象里面又可以包含children数组)

大致为很多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":
								 [
									
								 ]
							 ]
						}
					 ]
				}
			 ]
		}
	}
}

在这里插入图片描述
三;jsonCpp源码学习(如果解析出json文件数组进行存储到root中来让我们提取的)
等C++课程第三部分看完,及时填充上,以及解析XML格式文件和其源码

猜你喜欢

转载自blog.csdn.net/zw1996/article/details/108249996