• 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数据的就可以了。

    下面是我自己写的解析例子:

    #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,我更建议自己度娘一下,然后自己会更清楚点。

     这里有点需要注意:

    "whitelist": {
            "enable": "true",
            "urls": [
                {
                    "url": "http://www.baidu.com"
                },
                {
                    "url": "http://v.qq.com/"
                }
            ]
    	}
    

    待解析的json的以上部分如果改成:

    "whitelist": {
            "enable": "true",
            "urls": [
               
                    "http://www.baidu.com",
                    "url": "http://v.qq.com/"   
            ]
    	}
    

    改成这种后再用以前的解析,解析到这里会出问题,以前的在urls数组中的元素仍然是json对象,改后urls里的元素不再是json对象,而是一个个的字符串元素。解析思路还是不变的只要将代码中:

    tmp = arrayObj5[i]["url"].asString();
    

    改成:

    tmp = arrayObj5[i].asString();
    

    这样就可以了。

  • 相关阅读:
    JDK路径下目录的介绍及环境变量配置
    Eclipse显示或隐藏项目
    linux 上传文件 rz命令 提示command not found 解决方法
    Query failed with error code 96 and error message 'Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smal
    eclipse设置类和方的注释模板
    Xml 将一个Xml字节流写入到Dataset里面
    C# List<object> 转换为List<string>、转换为 string数组、转换为一个字符串用分隔符 隔开
    SQL 把m或f,更改为男女
    C# 去掉字符串里的分隔符(该分隔符可以是一个字符串)
    C# 通过一个开始时间和结束时间 按【AddDays】拆分成一个数组
  • 原文地址:https://www.cnblogs.com/DamonBlog/p/4273440.html
Copyright © 2020-2023  润新知