JsonCpp经典入门
来源:互联网 发布:政府网络信息安全 编辑:程序博客网 时间:2024/05/16 07:47
1.JsonCpp
1.1.JsonCpp简介
JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value pairs.
JsonCpp is a C++ library that allows manipulating JSON values, including serialization and deserialization to and from strings. It can also preserve existing comment in unserialization/serialization steps, making it a convenient format to store user input files.
1.2.JsonCpp环境搭建
1.2.1.下载地址
https://github.com/open-source-parsers/jsoncpp#generating-amalgamated-source-and-header
1.2.2.Qt中JsonCpp安装
① 解压jsoncpp-master.zip包
② 在根目录下,运行python amalgamate.py
③ 在根目录中生成dist文件夹包含三个文件dist/json/json-forwards.h dist/json/json.h dist/json.cpp
④ 在Qt工程目录下,生成json文件夹,并拷贝json目录下。
⑤ 在Qt工程中添加现有文件即可。
1.3.读写JsonCpp
1.3.1.写
#include <iostream>#include <fstream>#include "json/json.h"using namespace Json;using namespace std;#if 0{ "animals":{ "dog":[ { "name":"Rufus", "age":15 }, { "name":"Marty", "age":null } ] }}#endifvoid writeJson(){ Value rootAnimalsObj; Value dog; Value dogArray; Value dogValueItem1; dogValueItem1["name"] = "Rufus"; dogValueItem1["age"] = 15; Value dogValueItem2; dogValueItem2["name"] = "Marty"; dogValueItem2["age"] = ""; dogArray.append(dogValueItem1); dogArray.append(dogValueItem2); dog["dog"] = dogArray; rootAnimalsObj["animals"] = dog; string str = rootAnimalsObj.toStyledString(); cout<<str; ofstream ofs; ofs.open("test_write.json"); ofs << str; ofs.close(); return;}int main(){ writeJson(); return 0;}
1.3.2.读
#include <iostream>#include <fstream>#include "json/json.h"using namespace Json;using namespace std;#if 0{ "animals":{ "dog":[ { "name":"Rufus", "age":15 }, { "name":"Marty", "age":null } ] }}#endifvoid readJson(){ ifstream is("aa.json"); if(!is) { cout<<"open error"<<endl; return; } Reader reader; Value root; if(reader.parse(is,root)) { cout<<root<<endl; Value & dogArr = root["animals"]["dog"]; for(int i=0; i<dogArr.size(); i++) { cout<<dogArr[i]["name"].asString()<<endl; cout<<dogArr[i]["age"].asInt()<<endl; } } is.close();}int main(){ readJson(); return 0;}
1 0
- JsonCpp经典入门
- jsoncpp的入门学习
- JSONCPP
- jsoncpp
- jsoncpp
- JsonCpp
- jsoncpp
- VC下jsoncpp的使用入门(转)
- VC下jsoncpp的使用入门(转)
- json解析入门(jsoncpp库)
- 经典入门
- [推荐] C++解析json包:jsoncpp ——入门攻略
- Node.js入门经典-入门
- DataGrid入门经典
- Hibernate入门经典
- DataGrid入门经典(C#)
- Subversion入门(经典)
- DataGrid入门经典
- C++中的explicit关键字
- Fork/Join
- [poj 2376] Cleaning Shifts [最小区间覆盖 贪心]
- 【Java进阶】Java对象的强、软、弱、虚引用
- 字符串替换空格的两种方法
- JsonCpp经典入门
- java集合详解1
- 基于Scrapy框架的python网络爬虫学习(3)
- VS2013在Unicode字符集下读写ANSI编码文件
- MySQL索引类型总结和使用技巧以及注意事项
- SuperMap iClient for JavaScript 时空数据动画的实现(二)
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- MPU6050对卡尔曼、一阶互补滤波、二阶互补滤波的比较
- sax方式解析xml文档