刚工作不久,最近遇到一个要解析一个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();

这样就可以了。

jsoncpp用法通俗易懂之解析的更多相关文章

  1. jsoncpp用法通俗易懂之将数据合成json格式

    void *upload(void *pParam) { CUpSender *s = (CUpSender*)pParam; map<string, string> mx; char t ...

  2. C++ Json工具--Jsoncpp用法简介

    文章目录 Json简介 用法简介 数据类型 C++代码示例 代码执行输出结果 JSON在线解析及格式化验证 - JSON.cn Json简介 JSON(JavaScript Object Notati ...

  3. Python 中 -m 的典型用法、原理解析与发展演变

    在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下: python [-bBdEhiIOqsSuvVWx?] [-c command | -m module- ...

  4. javascript:void(0);用法及常见问题解析

    void 操作符用法格式: javascript:void (expression) 下面的代码创建了一个超级链接,当用户以后不会发生任何事.当用户链接时,void(0) 计算为 0,但 Javasc ...

  5. json简介及JsonCpp用法

    [时间:2017-04] [状态:Open] [关键词:数据交换格式,json,jsoncpp,c++,json解析,OpenSource] json简介 本文仅仅是添加我个人对json格式的理解,更 ...

  6. Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

  7. tinyXML的用法,用于解析gpx文件

    tinyxml是一个开源的C++xml解析工具集,简单.轻量而又高效,所以对于处理xml文件是一个非常不错的选择. 由于它开源,所以可以方便地免费下载,下载地址百度一下很容易找到,这里就不多说了. 下 ...

  8. springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore

    @SessionAttribute作用于处理器类上,用于在多个请求之间传递参数,类似于Session的Attribute,但不完全一样,一般来说@SessionAttribute设置的参数只用于暂时的 ...

  9. C++ Jsoncpp源代码编译与解析Json

    1.Json 数据表示方式介绍 这个可以看之前的一个文章里面有说明:Java解析(读取)Json数据 2.C++ Jsoncpp 2.1 Jsoncpp介绍 (1)JsonCpp主要包含三种类型的cl ...

随机推荐

  1. LeetCode Combinations (DFS)

    题意: 产生从1-n的k个数的所有组合,按升序排列并返回. 思路: DFS一遍即可解决.注意升序. class Solution { public: vector<vector<int&g ...

  2. php-多态

    <?php //面对对象三大特性//封装//目的:让类更安全//做法:成员变量变为私有的,通过方法来间接操作成员变量,在方法里面加限制条件 //继承//概念:子类可以继承父类的一切//方法重写: ...

  3. js实现元素添加样式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. nagios安装配置

     http://www.codeweblog.com/nagios%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE/ 上线的服务器有时会被人攻击,导致服务不可用,今天安装配置了 ...

  5. 三步搞定ISO/GHO安装系统 - imsoft.cnblogs

    高清互动安装系统附件:重装系统视频教程.7z

  6. Alice and Bob

    类似于石子合并的游戏,在黑板上写下N个数,每次只能将其中的一个数减1(结果为0自动消去),或者将某两个数消去,将其和写在黑板上. Alice先手,彼此都采用最优策略,将最后一个数消去者获胜. 思路:设 ...

  7. archlinux安装图形界面

    安装xorg 检查显卡驱动 $ lspci | grep VGA install X server wiki Then use the $ startx to start the X server, ...

  8. 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. List,set,Map 的用法和区别

    Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap Collecti ...

  10. ListView优化相关

    链接1 http://www.jb51.net/article/35273.htm 链接2 http://www.cnblogs.com/xilinch/archive/2012/11/08/2760 ...