【C++】JSON文件的读取和生成
来源:互联网 发布:最牛的妖股指标源码 编辑:程序博客网 时间:2024/06/06 02:47
一、从字符串中读取JSON
a.cpp
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
#include <iostream>
#include "json/json.h"
using
namespace
std;
int
main()
{
//字符串
const
char
* str =
"{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
"\"born\":-100,\"died\":-44}"
;
Json::Reader reader;
Json::Value root;
//从字符串中读取数据
if
(reader.parse(str,root))
{
string praenomen = root[
"praenomen"
].asString();
string nomen = root[
"nomen"
].asString();
string cognomen = root[
"cognomen"
].asString();
int
born = root[
"born"
].asInt();
int
died = root[
"died"
].asInt();
cout << praenomen +
" "
+ nomen +
" "
+ cognomen
<<
" was born in year "
<< born
<<
", died in year "
<< died << endl;
}
return
0;
}
makefile文件
1
2
3
4
5
6
7
8
LIB=-L
/usr/local/lib/libjson/
-ljson_linux-
gcc
-4.4.7_libmt
a: a.o
g++ -o a -std=c++0x a.o $(LIB)
a.o: a.cpp
g++ -c a.cpp
clean:
rm
-rf a.o a
运行结果
二、从文件中读取JSON
PersonalInfo.json(一个存储了JSON格式字符串的文件)
1
2
3
4
5
6
7
8
9
10
11
12
{
"name"
:
"Tsybius"
,
"age"
:23,
"sex_is_male"
:
true
,
"partner"
:
{
"partner_name"
:
"Galatea"
,
"partner_age"
:21,
"partner_sex_is_male"
:
false
},
"achievement"
:[
"ach1"
,
"ach2"
,
"ach3"
]
}
a.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include "json/json.h"
using
namespace
std;
int
main()
{
Json::Reader reader;
Json::Value root;
//从文件中读取
ifstream is;
is.open(
"PersonalInfo.json"
, ios::binary);
if
(reader.parse(is,root))
{
//读取根节点信息
string name = root[
"name"
].asString();
int
age = root[
"age"
].asInt();
bool
sex_is_male = root[
"sex_is_male"
].asBool();
cout <<
"My name is "
<< name << endl;
cout <<
"I'm "
<< age <<
" years old"
<< endl;
cout <<
"I'm a "
<< (sex_is_male ?
"man"
:
"woman"
) << endl;
//读取子节点信息
string partner_name = root[
"partner"
][
"partner_name"
].asString();
int
partner_age = root[
"partner"
][
"partner_age"
].asInt();
bool
partner_sex_is_male = root[
"partner"
][
"partner_sex_is_male"
].asBool();
cout <<
"My partner's name is "
<< partner_name << endl;
cout << (partner_sex_is_male ?
"he"
:
"she"
) <<
" is "
<< partner_age <<
" years old"
<< endl;
//读取数组信息
cout <<
"Here's my achievements:"
<< endl;
for
(
int
i = 0; i < root[
"achievement"
].size(); i++)
{
string ach = root[
"achievement"
][i].asString();
cout << ach <<
'\t'
;
}
cout << endl;
cout <<
"Reading Complete!"
<< endl;
}
is.close();
return
0;
}
makefile
1
2
3
4
5
6
7
8
LIB=-L
/usr/local/lib/libjson/
-ljson_linux-
gcc
-4.4.7_libmt
a: a.o
g++ -o a -std=c++0x a.o $(LIB)
a.o: a.cpp
g++ -c a.cpp
clean:
rm
-rf a.o a
运行结果
三、将信息保存为JSON格式
a.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include "json/json.h"
using
namespace
std;
int
main()
{
//根节点
Json::Value root;
//根节点属性
root[
"name"
] = Json::Value(
"Tsybius"
);
root[
"age"
] = Json::Value(23);
root[
"sex_is_male"
] = Json::Value(
true
);
//子节点
Json::Value partner;
//子节点属性
partner[
"partner_name"
] = Json::Value(
"Galatea"
);
partner[
"partner_age"
] = Json::Value(21);
partner[
"partner_sex_is_male"
] = Json::Value(
false
);
//子节点挂到根节点上
root[
"partner"
] = Json::Value(partner);
//数组形式
root[
"achievement"
].append(
"ach1"
);
root[
"achievement"
].append(
"ach2"
);
root[
"achievement"
].append(
"ach3"
);
//直接输出
cout <<
"FastWriter:"
<< endl;
Json::FastWriter fw;
cout << fw.write(root) << endl << endl;
//缩进输出
cout <<
"StyledWriter:"
<< endl;
Json::StyledWriter sw;
cout << sw.write(root) << endl << endl;
//输出到文件
ofstream os;
os.open(
"PersonalInfo"
);
os << sw.write(root);
os.close();
return
0;
}
makefile
1
2
3
4
5
6
7
8
LIB=-L
/usr/local/lib/libjson/
-ljson_linux-
gcc
-4.4.7_libmt
a: a.o
g++ -o a -std=c++0x a.o $(LIB)
a.o: a.cpp
g++ -c a.cpp
clean:
rm
-rf a.o a
运行结果
生成的文件PersonalInfo.json
1
2
3
4
5
6
7
8
9
10
11
{
"achievement"
: [
"ach1"
,
"ach2"
,
"ach3"
],
"age"
: 23,
"name"
:
"Tsybius"
,
"partner"
: {
"partner_age"
: 21,
"partner_name"
:
"Galatea"
,
"partner_sex_is_male"
:
false
},
"sex_is_male"
:
true
}
END
0 0
- 【C++】JSON文件的读取和生成
- 【C++】JSON文件的读取和生成
- 【C++】JSON文件的读取和生成
- 【C++】JSON文件的读取和生成
- json-c 的安装和使用(使用json-c读取文件中的json数据和保存json格式的数据至文件中)
- Java读取excel文件生成xml文档和JSON文档
- Java读取excel文件生成xml文档和JSON文档
- C 读取HTK生成的MFCC文件
- XML文件的解析、读取和生成(C/C++、win7、VS2010)
- TFRecords 文件的生成和读取
- C#读取excel文件,生成json
- C#读取excel文件,生成json
- 生成json文件和解析json文件
- 读取TXT文件,和生成TXT文件的方法
- 读取TXT文件,和生成TXT文件的方法
- XML文件和JSON文件的解析及生成
- jsp生成和读取文件
- 关于json和csb文件读取的问题
- java spring 开启SMTP服务发送QQ邮件
- python爬虫开发(6)—爬虫入门--Requests爬虫(session)
- 蓝牙资料备份
- 教你解决 Vim 粘贴缩进错乱问题
- 【最短路】CheapestRoute
- 【C++】JSON文件的读取和生成
- hdu 1024 Max Sum
- HDU 2034
- centos6.5(64)下apache+tomcat+memcached配置
- 虚拟机之无线连接的配置步骤
- shiro直接对类进行注解,类似于@Controller的形式
- Android自定义控件:时间滚轮控件的绘制
- hdu1070Milk
- hdu 1231 最大连续子序列