jsoncpp用法通俗易懂

刚工作不久,最近遇到一个要解析一个web服务器发过来的json格式的文件,文件如下:

{
    "global": {
        "renew": "true",
        "serverurl": "192.168.1.100:31208/opinfo/",
        "frequency": "60"
    },
    "auth": {
        "enable": "true",
        "url": "http://192.168.1.10:31208/authpage/13405/文件名"
    },
    "offline": {
        "enable": "true",
        "url": "http://192.168.1.10:31208/offinenotify",
        "parameters": "um#dm#ot#uf#df"
    },
    "jump": {
        "enable": "true",
        "url": "http://nav.cathay/index.html",
        "parameters": "un#dm#um#ui#di#jt"
    },
    "urlwrite": {
        "enable": "true",
		"uploadfrequency": "180",
		"uploadurl": "http://192.168.1.10:31208/uwdupload",
        "urls": [
            {
 				  "id": "1",
                "origin": "http://www.baidu.com",
                "purpose": "http://www.baidu.com?id=cathay",
                "terminal": "pc",
                "percent": "50"
            },
            {
				"id": "2",
                "origin": "http://www.soso.com",
                "purpose": "http://www.baidu.com?id=cathay",
                "terminal": "ios",
                "percent": "50"
            },
            {
				"id": "3",
                "origin": "http://www.google.com",
                "purpose": "http://www.baidu.com?id=cathay",
                "terminal": "android",
                "percent": "100"
            }
        ]
	},
	"whitelist": {
        "enable": "true",
        "urls": [
            {
                "url": "http://www.baidu.com"
            },
            {
                "url": "http://v.qq.com/"
            }
        ]
	}
}

解析一下这个json格式的文件
整个文件被一个大括号包含,相当于一个对象,在json中{}包含着的是对象
在这个大对象中又包含global、auth、offline、jump、urlwrite、whitelist这几个对象,这几个名字分别是对象名
像在global、auth、offline、jump中已经不存在对象,只有数据即:“名称:值”,这样的在大对象中取出global、auth、offline、jump这几个对象后就可以取值了
像urlwrite、whitelist这两个对象中又包含着对象,那么要在urlwrite、whitelist的基础上再取对象,然后再取值
这里在json中[]代表这是一个数组。

其实关键是你在处理之前先了解好json它的数据格式到底是什么样,再去了解jsoncpp是怎样解析json数据的就可以了。

下面是我自己写的解析demon:

#include "json/json.h"
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream is;
    is.open ("A1-F2-DF-Q2-DC.html", std::ios::binary );
     Json::Reader reader;
     Json::Value root;
	 cout << "beginning...." << endl;
     if(reader.parse(is,root))   
     {
		    cout << "将整个Json对象的value保存到root中去了" << endl;
			cout << endl;
		 	Json::Value arrayObj = root["global"];
		 	string tmp = arrayObj["renew"].asString();
			cout << "global---renew:" << tmp << endl;
		    tmp.clear();
			tmp = arrayObj["serverurl"].asString();
			cout << "global---serverurl:" << tmp << endl;
			tmp.clear();
			tmp = arrayObj["frequency"].asString();
			cout << "global---frequency:" << tmp << endl;
            tmp.clear();
			cout << endl;

			Json::Value arrayObj1 = root["auth"];
			tmp = arrayObj1["enable"].asString();
			cout << "auth---enable:" << tmp << endl;
			tmp.clear();
			tmp = arrayObj1["url"].asString();
			cout << "auth---url:" << tmp << endl;
			tmp.clear();
			cout << endl;
			
			Json::Value arrayObj2 = root["jump"];
			tmp = arrayObj2["enable"].asString();
			cout << "jump---enable" << tmp << endl;
			tmp.clear();
			tmp = arrayObj2["url"].asString();
			cout << "jump---url" << tmp << endl;
			tmp.clear();
			tmp = arrayObj2["parameters"].asString();
			cout << "jump---parameters" << tmp << endl;
			tmp.clear();
			cout << endl;
			
			Json::Value arrayObj3 = root["offline"];
			tmp = arrayObj3["enable"].asString();
			cout << "jump---enable" << tmp << endl;
			tmp.clear();
			tmp = arrayObj3["url"].asString();
			cout << "jump---url" << tmp << endl;
			tmp.clear();
			tmp = arrayObj3["parameters"].asString();
			cout << "jump---parameters" << tmp << endl;
			tmp.clear();
			cout << endl;
			
			Json::Value arrayObj4 = root["whitelist"];
			tmp = arrayObj4["enable"].asString();
			cout << "whitelist---enable:" << tmp << endl;
			tmp.clear();
			Json::Value arrayObj5 = arrayObj4["urls"];
			for(int i = 0; i != arrayObj5.size(); i++)
			{
				tmp = arrayObj5[i]["url"].asString();
				cout << "whitelist---url" << i << ":" << tmp << endl;
				tmp.clear();
			}
            
			Json::Value arrayObj6 = root["urlwrite"];
			tmp = arrayObj6["enable"].asString();
			cout << "urlwrite---enable:" << tmp << endl;
			tmp.clear();
			tmp = arrayObj6["uploadfrequency"].asString();
			cout << "urlwrite---uploadfrequency:" << tmp << endl;
			tmp = arrayObj6["uploadurl"].asString();
			cout << "urlwrite---uploadurl:" << tmp << endl;
			tmp.clear();
			Json::Value arrayObj7 = arrayObj6["urls"];
			for(int i = 0; i != arrayObj7.size(); i++)
			{
				tmp = arrayObj7[i]["id"].asString();
				cout << "urlwrite---urls---id:" << tmp << endl;
				tmp.clear();
				tmp = arrayObj7[i]["origin"].asString();
				cout << "urlwrite---urls---origin:" << tmp << endl;
				tmp.clear();
				tmp = arrayObj7[i]["purpose"].asString();
				cout << "urlwrite---urls---purpose:" << tmp << endl;
				tmp.clear();
				tmp = arrayObj7[i]["terminal"].asString();
				cout << "urlwrite---urls---terminal:" << tmp << endl;
				tmp.clear();
				tmp = arrayObj7[i]["percent"].asString();
				cout << "urlwrite---urls---percent:" << tmp << endl;
				tmp.clear();
			}
     }
     return 0;
}

编译操作:g++ c.cpp -I ./include/ libjson_linux-gcc-4.8_libmt.a

这里解析一下,include和.a文件都是编译jsoncpp生成的,至于怎么编译jsoncpp,我更建议自己度娘一下,然后自己会更清楚点。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。