Jsoncpp的使用
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
- “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
- 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
JSON具有以下这些形式:
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。
字符串(string)与C或者Java的字符串非常相似。

数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

空白可以加入到任何符号之间。 以下描述了完整的语言。
C++要使用JSON来解析数据,一般采用jsoncpp.
网站:http://sourceforge.net/projects/jsoncpp/
下载了之后,解压,然后打开\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71
下的工程文件,进行编译链接就可以得到一个静态链接库json.lib
要用jsoncpp只需要将这个lib文件拷贝到你的工程目录下,并将jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\json
复制到工程目录下,然后将这些头文件加到工程中去就可以了。
例子:
{
"name" : "小楼一夜听春雨",
"age" : 27
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#pragma comment(lib, "json_mtd.lib")#include <fstream>#include <cassert>#include "json/json.h"int main(){ ifstream ifs; ifs.open("testjson.json"); assert(ifs.is_open()); Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root, false)) { return -1; } std::string name = root["name"].asString(); int age = root["age"].asInt(); std::cout<<name<<std::endl; std::cout<<age<<std::endl; return 0;} |
[{"name" : "xiaoy", "age" :17} , {"name" : "xiaot", "age" : 20}]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#pragma comment(lib, "json_mtd.lib")#include <fstream>#include <cassert>#include "json/json.h"int main(){ ifstream ifs; ifs.open("testjson.json"); assert(ifs.is_open()); Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root, false)) { return -1; } std::string name; int age; int size = root.size(); for (int i=0; i<size; ++i) { name = root[i]["name"].asString(); age = root[i]["age"].asInt(); std::cout<<name<<" "<<age<<std::endl; } return 0;} |
json写入:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#pragma comment(lib, "json_mtd.lib")#include <fstream>#include <cassert>#include "json/json.h"int main(){ Json::Value root; Json::FastWriter writer; Json::Value person; person["name"] = "hello world"; person["age"] = 100; root.append(person); std::string json_file = writer.write(root); ofstream ofs; ofs.open("test1.json"); assert(ofs.is_open()); ofs<<json_file; return 0;} |
结果:[{"age":100,"name":"hello world"}]
json对数组的解析还支持STL的风格。即
Json::Value::Members member;//Members 这玩意就是vector<string>,typedef了而已
for (Json::Value::iterator itr = objArray.begin(); itr != objArray.end(); itr++)
{
member = (*itr).getMemberNames();
for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); iter++)
{
string str_temp = (*itr)[(*iter)].asString();
}
}
此种风格与上面的类似,只是上面的只是取"text"节点,而后一种是输出所有节点。
Jsoncpp的使用的更多相关文章
- C++处理Json串——jsoncpp库
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似,本文主要对VS2008中使用Jsoncpp解析json的方法做一下记录.Jsoncpp是个跨 ...
- Json---使用Jsoncpp解析与写入
上述Json解析使用的是Jsoncpp,要使用Jsoncpp,得做如下几步的配置: 1.首先从http://sourceforge.net/projects/jsoncpp/下载,压缩包大约105k. ...
- 【003:jsoncpp的简单使用】
#include <json/json.h> #include <iostream> #include <string> using namespace std; ...
- jsoncpp初使用
一 前言 由于最近一个c++项目需要使用json这种数据格式来传输数据, so上网去寻找合适的类库,毕竟对于这种不是很新的技术, 自己造轮子有点得不偿失. 从百度上翻了翻, 基本上就boost跟jso ...
- Jsoncpp Compiler、Programming
catalog . C++ jsoncpp简介 . Jsoncpp的下载与编译 . Linux Jsoncpp的SDK编译 & 简单实例 . Windows Jsoncpp的SDK编译 &am ...
- jsoncpp操作 json
jsoncpp操作 json 博客分类: c/c++ object-c 之 iphone #include <iostream> //#include "json/json. ...
- JsonCpp简单使用
作者:ilife JsonCpp简单使用 1.相关概念总结 (1)解析json的方法 Json::Value json; //表示一个json格式的对象 Json::Reader reader ...
- JsonCpp的简单使用方法
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- C++ JsonCpp 使用(含源码下载)
C++ JsonCpp 使用(含源码下载) 前言 JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择JSON的全称为:JavaScri ...
- C++之jsoncpp学习
最新由于客户端要用到jsoncpp,所以自己也跟着项目的需求学了一下jsoncpp.以前没用过xml,但是感觉接触json后,还蛮好用的. 参考地址 http://jsoncpp.sourceforg ...
随机推荐
- LoadRunner常见问题
1.Error -27257: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) detected and r ...
- IIS32位,64位模式下切换
一.32位模式 1.cscript %systemdrive%\inetpub\adminscripts\adsutil.vbs set w3svc/appPools/enable32bitappon ...
- 关于 Graph Convolutional Networks 资料收集
关于 Graph Convolutional Networks 资料收集 1. GRAPH CONVOLUTIONAL NETWORKS ------ THOMAS KIPF, 30 SEPTE ...
- dk.internal.org.objectweb.asm.Opcodes.IF_ACMPNE
http://cr.openjdk.java.net/~hannesw/8008351/webrev/src/jdk/nashorn/internal/codegen/Condition.java.s ...
- Winform中调用js函数
var wb = new WebBrowser(); wb.AllowNavigation = true; wb.ScriptErrorsSuppressed = false; wb.Navigate ...
- 什么是 Web API
http://www.cnblogs.com/developersupport/p/aspnet-webapi.html Web API 强势入门指南 Web API是一个比较宽泛的概念.这里我们提到 ...
- WebApi:WebApi的Self Host模式
不用IIS也能執行ASP.NET Web API 转载:http://blog.darkthread.net/post-2013-06-04-self-host-web-api.aspx 在某些情境, ...
- 递归绑定将数据表中的数据按层级更新到 TreeView节点上
private void bindTreeView1() { string sql = "select * from dm_category&quo ...
- su和su-命令的本质区别
su命令和su -命令最大的本质区别就是:前者只是切换了root身份,但Shell环境仍然是普通用户的Shell: 而后者连用户和Shell环境一起切换成root身份了.只有切换了Shell环境才不会 ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...