C++操作Json字符串
一、从字符串中读取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
C++操作Json字符串的更多相关文章
- JS 操作JSON字符串
用Js的eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eva ...
- 使用 Newtonsoft.Json 操作 JSON 字符串
一.把实体类转化为 JSON 字符串 1. 为实体类赋值 SenderFromMQSearch senderFromMQSearch = new SenderFromMQSearch(); sende ...
- Newtonsoft.Json 操作 JSON 字符串
Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataContractJsonSeriali ...
- 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何
基于 Vue.js 之 iView UI 框架非工程化实践记要 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...
- java操作JSON字符串转换成对象的时候如何可以不建立实体类也能获取数据
引入依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson& ...
- 让C#可以像Javascript一样操作Json
Json的简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了 ...
- C# 技巧(3) C# 操作 JSON
RestAPI中, 经常需要操作json字符串, 需要把json字符串"反序列化"成一个对象, 也需要把一个对象"序列化"成一字符串. C# 操作json, ...
- .NET操作JSON
http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html JSON文件读入到内存中就是字符串,.NET操作JSON就是生成与 ...
- C#操作JSON学习
JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...
随机推荐
- Servlet到Servlet的请求转发与重定向的区别
Servlet跳转到另一个Servlet中: request.getRequestDispatcher().forward();代表默认的是post方法,即在被跳转的Servlet的doPost()方 ...
- js基础---数字日期及运算
显示年月日 var a=new Date; console.log(a); var year=a.getFullYear(); var month=a.getMonth()+1; var day=a. ...
- ThinkPHP的基础使用
最近学习了ThinkPHP框架写页面,趁着夜色写写自己的一些经验: 我这里用的服务器是phpStudy,数据库是Navicat,项目必须放在phpStudy的WWW目录里面. 1,搭建th项目 1.将 ...
- drupal 8 ——自定义权限
在项目开发里面,我遇到了这么一个需求,就是对于node的title字段,编辑内容的角色不允许对title进行编辑.title字段是创建内容类型时自动生成的字段,不能在drupal8后台直接配置权限,所 ...
- Angular——引入模板指令
基本介绍 引入模板一般都是固定的东西,比如导航栏,比如页面的底部,每个页面都重复写很麻烦,不如直接定义两个模板,引入到需要的页面中.这个过程实际是一个跨域的异步请求过程. 基本使用 <!DOCT ...
- [Windows Server 2003] 安装SQL Server 2005
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...
- (转)Struts2的标签库
http://blog.csdn.net/yerenyuan_pku/article/details/68638679 Struts2的标签库 对于一个MVC框架而言,重点是实现两部分:业务逻辑控制器 ...
- 微信 之jsapi实现支付
一.微信公众号号后台支付配置 附微信支付参考文档:https://pay.weixin.qq.com/wiki/doc/api/index.html 二.微信支付类封装 该类可以实现付款码支付.JSA ...
- 【原创】使用HTML5+canvas+JavaScript开发的原生中国象棋游戏及源码分享
目前已经实现的功能: V1.0 : 实现棋子的布局,画布及游戏场景的初始化V2.0 : 实现棋子的颜色改变V3.0 :实现所有象棋的走棋规则V4.0 : 实现所有棋子的吃子功能 GItHub源码下载地 ...
- SQL上门
学习这个 介绍:SQL 是用于访问和处理数据库的标准的计算机语言.结构化化查询语言! SQL可以分为两大部分:数据操作语言(DML)和数据定义语言(DDL) 数据操作语言:select.update. ...