刚工作不久,最近遇到一个要解析一个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. C++学习笔记1:高级语言

    笔记参考资料:http://www.learncpp.com/ 1.high languages:C,C++,Pascal,Java,Javascript,Perl... compiling proc ...

  2. Git工作常用

    工作时,最好每次都创建一个本地分支,这样就会减少错误修改远程代码的机会. 基于远程dev分支创建分支并切到本地分支: git checkout -b <local_dev> -t orig ...

  3. 《JavaScript Ninja》之闭包

    闭包 闭包是什么,它们是如何工作的 闭包 是一个函数在创建时允许该自身函数访问并操作该自身函数之外的变量时所创建的作用域. 即:闭包可以让函数访问所有的变量和函数,只要这些变量和函数存在于该函数声明时 ...

  4. dede cms列表页调用文章简介(借鉴)

    {dede:list pagesize='8' infolen='50'}<a href="[field:arcurl/]">[field:title/][内容调用代码 ...

  5. 支持 Firefox、Chrome 等主流浏览器的全站变灰 CSS 代码

    <style> html{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grays ...

  6. POJ 1523 SPF(寻找关节点)

                                                                         SPF Time Limit: 1000MS   Memory ...

  7. Linux驱动设计—— 中断与时钟@request_irq参数详解

    request_irq函数定义 /*include <linux/interrupt.h>*/ int request_irq(unsigned int irq, irq_handler_ ...

  8. Javascript中最常用的61段经典代码

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...

  9. php strtotime 和 date 日期操作

    time()得到的数值是1970-1-1到现在的秒数,strtotime("now")也是,两个值是相同的. http://doc.thinkphp.cn/manual/syste ...

  10. m3u8文件简介

    1.   M3U8文件概念 M3U8文件是指UTF-8编码格式的M3U文件.M3U文件是记录了一个索引纯文本文件,打开它时播放软件并不是播放它,而是根据它的索引找到对应的音视频文件的网络地址进行在线播 ...