json c++库还是有很多的,因为工作上经常使用boost,这里选用boost的json,记录下用法。

举个栗子:

如果我们要生成如下格式的json:

{
"name":"jim",
"info":
{
"weight":"",
"all_phone":
[
{
"phone":""
},
{
"phone":""
}
]
}
}

解析和生成的示例代码如下

 #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <vector>
using namespace boost::property_tree;
using namespace std;
bool CreateJson(wstring &wstr)
{
wstringstream wstream;
try
{
wptree pt;
pt.put(L"name",L"jim");
wptree info;
info.put(L"weight",L"");
wptree phone,phone_item1,phone_item2;
phone_item1.put(L"phone",L"");
phone_item2.put(L"phone",L"");
phone.push_back(make_pair(L"",phone_item1));
phone.push_back(make_pair(L"",phone_item2));
info.put_child(L"all_phone",phone);
pt.push_back(make_pair(L"info",info));
write_json(wstream,pt); }
catch(ptree_error pt)
{
pt.what();
return false;
}
wstr = wstream.str();
return true;
}
bool ParseJson(wstring &wstr)
{
try
{
wptree pt;
wstringstream wstream(wstr);
read_json(wstream,pt);
wstring wstrName = pt.get<wstring>(L"name");
wptree info = pt.get_child(L"info");
wstring weight = info.get<wstring>(L"weight");
int w=;
w=info.get<int>(L"weight");
wptree phones = info.get_child(L"all_phone");
vector<wstring>vcPhone;
BOOST_FOREACH(wptree::value_type &v,phones)
{
vcPhone.push_back(v.second.get<wstring>(L"phone"));
}
}
catch(ptree_error pt)
{
pt.what();
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
wstring wstr;
CreateJson(wstr);
ParseJson(wstr);
return ;
}

用法还是很简单的

这里需要注意的是:

1 boost json不支持空数组,在本例中空数组对应的格式为"all_phone":"";

2  空的字符串字段转换为数字会抛异常。

boost json生成和解析用法的更多相关文章

  1. Android Json生成及解析实例

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  2. DELPHI XE10,JSON 生成和解析,再利用INDYHTTP控件POST

    Delphi XE10,Json 生成和解析,再利用indyhttp控件Post 年09月20日 :: 阅读数: --不多说,直接上代码 procedure TFrmMain.Brand; var J ...

  3. Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  4. 【转载】Qt之JSON生成与解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...

  5. Json生成与解析

    JSON常用与服务器进行数据交互,JSON中"{}"表示JSONObject,"[]"表示JSONArray 如下json数据: {"singers& ...

  6. delphi c++builder JSON 生成与解析 例子

    json,System.JSON,REST.JSON JSON有两种数据结构,对象和数组. 对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...} 数组 ...

  7. fastjson生成和解析json数据,序列化和反序列化数据

    本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...

  8. fastjson生成和解析json数据

    本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...

  9. Boost.JSON Boost的JSON解析库(1.75首发)

    目录 目录 Boost的1.75版本新库 JSON库简介 JSON的简单使用 编码 最通用的方法 使用std::initializer_list json对象的输出 两种对比 解码 简单的解码 增加错 ...

随机推荐

  1. Parallelogram Counting(平行四边形个数,思维转化)

    1058 - Parallelogram Counting    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  2. Fire Net(dfs)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. Dictionary到List转换中的性能问题 转

    本文来自:http://www.cnblogs.com/353373440qq/p/3488367.html 在应用泛型中,我们经常使用Dictionary,经常会用到Dictionary到List的 ...

  4. twitter 监控登陆活动

    http://vicenteaguileradiaz.com/download/tinfoleak/tinfoleak-1.2.tar.gz

  5. 如何一步步把网站Retina优化

    随着高清屏幕.高分辨率屏幕越来越流行,例如MacBook Retina机型.iPad Air系列,这些新生机器有着很高的PPI,对网页的清晰度要求很高,所以越来越多的站长都不得不面临一个问题,那就是把 ...

  6. sql显示12个月数据

    需求 最近在做一个财务报表展示系统,Budget需要当月上传,还未上传月份的数据也需要显示出来. 数据库设计 cBudget表结构如下 CREATE TABLE [dbo].[cBudget]( ,) ...

  7. ListView及Adapter的使用

    一.使用ArrayAdapter 其中ArrayAdapter的构造函数有如下几个,其中resource是指每个列表项的布局文件,objects是指列表项的数据源,此处通常指一个数组 ArrayAda ...

  8. .net 生成缩略图

    public static void CreateSmallImage(string minImageFullPath, System.Drawing.Image originalImage, int ...

  9. git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚

    [git 删除本地分支] git branch -D br [git 删除远程分支] git push origin :br  (origin 后面有空格) git代码库回滚: 指的是将代码库某分支退 ...

  10. WebStorm11 注册码

    这个注册方法我也是在贴吧上看到的:http://tieba.baidu.com/p/4160766032 注册方法 注册时选择“License server”输入“http://15.idea.lan ...