Jsoncpp的使用
来源:互联网 发布:软件项目验收清单 编辑:程序博客网 时间:2024/05/14 19:05
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
}
#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}]
#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写入:
#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的风格。即
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的使用
- Jsoncpp的简单使用
- Jsoncpp的使用
- JsonCpp的使用
- Jsoncpp的使用
- Jsoncpp的使用
- Jsoncpp的使用
- jsoncpp的安全使用
- Jsoncpp的使用
- Jsoncpp的使用
- jsoncpp 的使用
- jsoncpp的使用
- Jsoncpp的使用
- Jsoncpp的使用(转)
- Jsoncpp的使用
- Jsoncpp的使用
- Jsoncpp的使用
- jsoncpp的使用
- java概念
- 时间序列分析之 ARIMA 模型的JAVA实现
- LeetCode 3Sum
- STRUTS框架(web.xml,struts.xml,XXXvalidation.xml配置信息)个人心得
- android activity(通过一个按钮打开另一个acticity总结)
- Jsoncpp的使用
- 回调函数和接口回调的理解,有图更给力!
- 解决Ubuntu中update的问题(Reading package lists... Error!)
- MFC 视图类(CView)介绍
- 常用的Java代码汇总
- 移动支付的安全问题
- ubuntu防火墙在系统启动时自动禁用
- 转载- expdp中TABLE_EXISTS_ACTION参数
- YT14——先来练练手(1)