JsonCpp使用方法详解
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp是c++解析JSON串常用的解析库之一。
jsoncpp中主要的类:
Json::Value:可以表示所有支持的类型,如:int , double ,string , object, array等。其包含节点的类型判断(isNull,isBool,isInt,isArray,isMember,isValidIndex等),类型获取(type),类型转换(asInt,asString等),节点获取(get,[]),节点比较(重载<,<=,>,>=,==,!=),节点操作(compare,swap,removeMember,removeindex,append等)等函数。
Json::Reader:将文件流或字符串创解析到Json::Value中,主要使用parse函数。Json::Reader的构造函数还允许用户使用特性Features来自定义Json的严格等级。
Json::Writer:与JsonReader相反,将Json::Value转换成字符串流等,Writer类是一个纯虚类,并不能直接使用。在此我们使用 Json::Writer 的子类:Json::FastWriter(将数据写入一行,没有格式),Json::StyledWriter(按json格式化输出,易于阅读)。
Json::Reader可以通过对Json源目标进行解析,得到一个解析好了的Json::Value,通常字符串或者文件输入流可以作为源目标。
如下Json文件example.json:
- {
- "encoding" : "UTF-8",
- "plug-ins" : [
- "python",
- "c++",
- "ruby"
- ],
- "indent" : { "length" : 3, "use_space": true }
- "tab":null
- }
使用Json::Reader对Json文件进行解析:
- Json::Value root;
- Json::Reader reader;
- std::ifstream ifs("example.json");//open file example.json
- if(!reader.parse(ifs, root)){
- // fail to parse
- }
- else{
- // success
- std::cout<<root["encoding"].asString()<<endl;
- std::cout<<root["indent"]["length"].asInt()<<endl;
- }
使用Json::Reader对字符串进行解析:
- Json::Value root;
- Json::Reader reader;
- const char* s = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";
- if(!reader.parse(s, root)){
- // "parse fail";
- }
- else{
- std::cout << root["uploadid"].asString();//print "UP000000"
- }
Json::Writer 和 Json::Reader相反,是把Json::Value对象写到string对象中,而且Json::Writer是个抽象类,被两个子类Json::FastWriter和Json::StyledWriter继承。
简单来说FastWriter就是无格式的写入,这样的Json看起来很乱没有格式,而StyledWriter就是带有格式的写入,看起来会比较友好。
- Json::Value root;
- Json::Reader reader;
- Json::FastWriter fwriter;
- Json::StyledWriter swriter;
- if(! reader.parse("example.json", root)){
- // parse fail
- return 0;
- }
- std::string str = fwriter(root);
- std::ofstream ofs("example_fast_writer.json");
- ofs << str;
- ofs.close();
- str = swriter(root);
- ofs.open("example_styled_writer.json");
- ofs << str;
- ofs.close();
- 结果1:example_styled_writer.json:
- {
- "encoding" : "UTF-8",
- "plug-ins" : [
- "python",
- "c++",
- "ruby"
- ],
- "indent" : { "length" : 3, "use_space": true }
- "tab":null
- }
- 结果2:example_fast_writer.json:
- {"encoding" : "UTF-8","plug-ins" : ["python","c++","ruby"],"indent" : { "length" : 3, "use_space": true}}
Json其它函数的应用:
1、判断KEY值是否存在:
- if(root.isMember("encoding")){
- std::cout<<"encoding is a member"<<std::endl;
- }
- else{
- std::cout<<"encoding is not a member"<<std::endl;
- }
2、判断Value是否为null:
if(root["tab"].isNull()){
std::cout << "isNull" <<std::endl;//print isNull
}
完整例子使用举例来自于CSDN下载网友的程序:
源码下载地址:http://download.csdn.net/download/woniu211111/9966907
- /********************************************************
- Copyright (C), 2016-2017,
- FileName: main
- Author: woniu201
- Email: wangpengfei.201@163.com
- Created: 2017/09/06
- Description:use jsoncpp src , not use dll, but i also provide dll and lib.
- ********************************************************/
- #include "stdio.h"
- #include <string>
- #include "jsoncpp/json.h"
- using namespace std;
- /************************************
- @ Brief: read file
- @ Author: woniu201
- @ Created: 2017/09/06
- @ Return: file data
- ************************************/
- char *getfileAll(char *fname)
- {
- FILE *fp;
- char *str;
- char txt[1000];
- int filesize;
- if ((fp=fopen(fname,"r"))==NULL){
- printf("open file %s fail \n",fname);
- return NULL;
- }
- /*
- 获取文件的大小
- ftell函数功能:得到流式文件的当前读写位置,其返回值是当前读写位置偏离文件头部的字节数.
- */
- fseek(fp,0,SEEK_END);
- filesize = ftell(fp);
- str=(char *)malloc(filesize);
- str[0]=0;
- rewind(fp);
- while((fgets(txt,1000,fp))!=NULL){
- strcat(str,txt);
- }
- fclose(fp);
- return str;
- }
- /************************************
- @ Brief: write file
- @ Author: woniu201
- @ Created: 2017/09/06
- @ Return:
- ************************************/
- int writefileAll(char* fname,const char* data)
- {
- FILE *fp;
- if ((fp=fopen(fname, "w")) == NULL)
- {
- printf("open file %s fail \n", fname);
- return 1;
- }
- fprintf(fp, "%s", data);
- fclose(fp);
- return 0;
- }
- /************************************
- @ Brief: parse json data
- @ Author: woniu201
- @ Created: 2017/09/06
- @ Return:
- ************************************/
- int parseJSON(const char* jsonstr)
- {
- Json::Reader reader;
- Json::Value resp;
- if (!reader.parse(jsonstr, resp, false))
- {
- printf("bad json format!\n");
- return 1;
- }
- int result = resp["Result"].asInt();
- string resultMessage = resp["ResultMessage"].asString();
- printf("Result=%d; ResultMessage=%s\n", result, resultMessage.c_str());
- Json::Value & resultValue = resp["ResultValue"];
- for (int i=0; i<resultValue.size(); i++)
- {
- Json::Value subJson = resultValue[i];
- string cpuRatio = subJson["cpuRatio"].asString();
- string serverIp = subJson["serverIp"].asString();
- string conNum = subJson["conNum"].asString();
- string websocketPort = subJson["websocketPort"].asString();
- string mqttPort = subJson["mqttPort"].asString();
- string ts = subJson["TS"].asString();
- printf("cpuRatio=%s; serverIp=%s; conNum=%s; websocketPort=%s; mqttPort=%s; ts=%s\n",cpuRatio.c_str(), serverIp.c_str(),
- conNum.c_str(), websocketPort.c_str(), mqttPort.c_str(), ts.c_str());
- }
- return 0;
- }
- /************************************
- @ Brief: create json data
- @ Author: woniu201
- @ Created: 2017/09/06
- @ Return:
- ************************************/
- int createJSON()
- {
- Json::Value req;
- req["Result"] = 1;
- req["ResultMessage"] = "200";
- Json::Value object1;
- object1["cpuRatio"] = "4.04";
- object1["serverIp"] = "42.159.116.104";
- object1["conNum"] = "1";
- object1["websocketPort"] = "0";
- object1["mqttPort"] = "8883";
- object1["TS"] = "1504665880572";
- Json::Value object2;
- object2["cpuRatio"] = "2.04";
- object2["serverIp"] = "42.159.122.251";
- object2["conNum"] = "2";
- object2["websocketPort"] = "0";
- object2["mqttPort"] = "8883";
- object2["TS"] = "1504665896981";
- Json::Value jarray;
- jarray.append(object1);
- jarray.append(object2);
- req["ResultValue"] = jarray;
- Json::FastWriter writer;
- string jsonstr = writer.write(req);
- printf("%s\n", jsonstr.c_str());
- writefileAll("createJson.json", jsonstr.c_str());
- return 0;
- }
- int main()
- {
- /*读取Json串,解析Json串*/
- char* json = getfileAll("parseJson.json");
- parseJSON(json);
- printf("===============================\n");
- /*组装Json串*/
- createJSON();
- getchar();
- return 1;
- }
参考:
http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html
http://blog.csdn.net/yc461515457/article/details/52749575
JsonCpp使用方法详解的更多相关文章
- session的使用方法详解
session的使用方法详解 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台WWW服务器 ...
- Kooboo CMS - Html.FrontHtml[Helper.cs] 各个方法详解
下面罗列了方法详解,每一个方法一篇文章. Kooboo CMS - @Html.FrontHtml().HtmlTitle() 详解 Kooboo CMS - Html.FrontHtml.Posit ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
- ecshop后台增加|添加商店设置选项和使用方法详解
有时候我们想在Ecshop后台做个设置.radio.checkbox 等等来控制页面的显示,看看Ecshop的设计,用到了shop_config这个商店设置功能 Ecshop后台增加|添加商店设置选项 ...
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- windows.open()、close()方法详解
windows.open()方法详解: window.open(URL,name,features,replace)用于载入指定的URL到新的或已存在的窗口中,并返回代表新窗口的Win ...
- CURL使用方法详解
php采集神器CURL使用方法详解 作者:佚名 更新时间:2016-10-21 对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
随机推荐
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}...
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
- Keil 中文显示乱码解决办法
在将代码文件转换成UTF-8之前还要把Keil的环境也设置成UTF-8的模式,方法是:“Edit”——〉“Configuration...”——〉“Encoding”,选择“Encode in UTF ...
- java 需要看的书籍
参考链接:http://www.jianshu.com/p/454fc1e6cbe2 最近要看的有:Effective java 深入理解java 虚拟机 java 并发编程实战 (设计模式的书籍 ...
- NodeJS 难点(网络,文件)的 核心 stream 二:stream是什么
对于大部分有后端经验的的同学来说 Stream 对象是个再合理而常见的对象,但对于前端同学 Stream 并不是那么理所当然,github 上甚至有一篇 9000 多 Star 的文章介绍到底什么是 ...
- zabbix 爆高危 SQL 注入漏洞,可获系统权限(profileIdx 2 参数)
漏洞概述 zabbix是一个开源的企业级性能监控解决方案.近日,zabbix的jsrpc的profileIdx2参数存在insert方式的SQL注入漏洞,攻击者无需授权登陆即可登陆zabbix管理系统 ...
- vue|html5 form 验证
html:<form id="scoreForm" @submit="fsub" > <template v-for="(item, ...
- ldconfig
#ldconfig# http://www.cnblogs.com/lyongde/p/4190588.html ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链 ...
- 关于Gson无法将匿名类转化为json字符串的问题
在使用gson过程中,一般会将数据存在一个对象模型中,使用gson将模型转换成json字符串用于数据交互. 代码形如: ArrayList<String> list = new Array ...
- 【linux基础】重命名文件和文件夹
linux下重命名文件或文件夹的命令mv既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 其实在文本模式 ...
- Plotly绘图工具(多用于统计)
作者:桂. 时间:2017-04-23 23:52:14 链接:http://www.cnblogs.com/xingshansi/p/6754769.html 前言 无意中考到一个小工具,网址为: ...