7、JsonCpp简单使用(3)
2011-11-08 20:47
330 查看
1、相关概念总结
(1)解析json的方法
Json::Value json; //表示一个json格式的对象
Json::Reader reader; //json解析
reader.parse(json_buf/*json格式的字符串*/,json,false); //解析出json放到json中
jsoncpp库中的Reader类用来将字串或者流载入解析器。后期可以用Reader里面的解析方法把Json字串解码为C++认识的数据。可以用 Json::Reader来声明一个Reader实例。Reader中最常用的就是一个parse方法,该方法用来将载入的json字串解析为C++格式的数据。
(2) 数组访问
Json::Value //格式如下
[["key1":value1],["key2":value2]]
Json::Value::const_iterator iter; //迭代器
for(iter = input.begin(); iter != input.end(); iter++)
Json::Value::Members member=(*iter).getMemberNames();
*(member.begin()); // 输出 key1,key2
(*iter)[*(member.begin())]; //输出 value1,value2
Value类是库中的核心类,用于存储各样格式的数据,可以包括int,double,short,char *,string,bool,object,array等几乎所有格式的数据。该库的编码和解码的核心功能都是用Value类实现的。就用以上的 Reader的parse方法来说,需要传入一个Value类别的引用值,就是用来存储Json数据的根值,并且可以用这个根值来存取其他的所有值。
(3) 对象访问
直接用 value["key"]即可
(4) 输出json格式串
调用 Json::FastWriter的writer
writer是该库的一个虚类,没有真正的实现encode的功能。需要重载里头的方法来实现真正的encode功能。FastWriter是该库中真正实现encode功能的类,用来实现将Value编码称为Json串。Json::StyledWriter 是格式化后的json。
不支持utf-8格式的输出,需要自己调用writer之后,用iconv转化成utf-8字符串
2、示例代码
1)示例代码1
View Code
参考
【1】 讲解说明
http://hi.baidu.com/%B4%AB%CB%B5%D6%D0%B5%C4%C8%CC%D5%DF%C3%A8/blog/item/a6eb970c98a644d67acbe15a.html
【2】 示例,该作者博文不错,涵盖了各个方面
http://hi.baidu.com/s_jqzhang/blog/item/a3c5df1f9408246ff624e4f5.html/cmtid/02e72e4fcb488039aec3ab28
【3】 boost库支持json比较好
http://freedomhui.com/?p=6
【3】 对json的类型作了简单的小结,为json-c进行了介绍
http://developer.51cto.com/art/201001/176060.htm
【4】 官网
http://jsoncpp.sourceforge.net/index.html
相关的类介绍及使用
http://jsoncpp.sourceforge.net/annotated.html
【5】 其他例子
http://joysofprogramming.com/json_parser_json-c/
http://forum.openframeworks.cc/index.php?topic=2833.0
(1)解析json的方法
Json::Value json; //表示一个json格式的对象
Json::Reader reader; //json解析
reader.parse(json_buf/*json格式的字符串*/,json,false); //解析出json放到json中
jsoncpp库中的Reader类用来将字串或者流载入解析器。后期可以用Reader里面的解析方法把Json字串解码为C++认识的数据。可以用 Json::Reader来声明一个Reader实例。Reader中最常用的就是一个parse方法,该方法用来将载入的json字串解析为C++格式的数据。
(2) 数组访问
Json::Value //格式如下
[["key1":value1],["key2":value2]]
Json::Value::const_iterator iter; //迭代器
for(iter = input.begin(); iter != input.end(); iter++)
Json::Value::Members member=(*iter).getMemberNames();
*(member.begin()); // 输出 key1,key2
(*iter)[*(member.begin())]; //输出 value1,value2
Value类是库中的核心类,用于存储各样格式的数据,可以包括int,double,short,char *,string,bool,object,array等几乎所有格式的数据。该库的编码和解码的核心功能都是用Value类实现的。就用以上的 Reader的parse方法来说,需要传入一个Value类别的引用值,就是用来存储Json数据的根值,并且可以用这个根值来存取其他的所有值。
(3) 对象访问
直接用 value["key"]即可
(4) 输出json格式串
调用 Json::FastWriter的writer
writer是该库的一个虚类,没有真正的实现encode的功能。需要重载里头的方法来实现真正的encode功能。FastWriter是该库中真正实现encode功能的类,用来实现将Value编码称为Json串。Json::StyledWriter 是格式化后的json。
不支持utf-8格式的输出,需要自己调用writer之后,用iconv转化成utf-8字符串
2、示例代码
1)示例代码1
View Code
Json::Value root; // will contains the root value after parsing. Json::Reader reader; bool parsingSuccessful = reader.parse( config_doc, root ); if ( !parsingSuccessful ) { // report to the user the failure and their locations in the document. std::cout << "Failed to parse configuration\n" << reader.getFormattedErrorMessages(); return; } // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no // such member. std::string encoding = root.get("encoding", "UTF-8" ).asString(); // Get the value of the member of root named 'encoding', return a 'null' value if // there is no such member. const Json::Value plugins = root["plug-ins"]; for ( int index = 0; index < plugins.size(); ++index ) // Iterates over the sequence elements. loadPlugIn( plugins[index].asString() ); setIndentLength( root["indent"].get("length", 3).asInt() ); setIndentUseSpace( root["indent"].get("use_space", true).asBool() ); // ... // At application shutdown to make the new configuration document: // Since Json::Value has implicit constructor for all value types, it is not // necessary to explicitly construct the Json::Value object: root["encoding"] = getCurrentEncoding(); root["indent"]["length"] = getCurrentIndentLength(); root["indent"]["use_space"] = getCurrentIndentUseSpace(); Json::StyledWriter writer; // Make a new JSON document for the configuration. Preserve original comments. std::string outputConfig = writer.write( root ); // You can also use streams. This will put the contents of any JSON // stream at a particular sub-value, if you'd like. std::cin >> root["subtree"]; // And you can write to a stream, using the StyledWriter automatically. std::cout << root;
参考
【1】 讲解说明
http://hi.baidu.com/%B4%AB%CB%B5%D6%D0%B5%C4%C8%CC%D5%DF%C3%A8/blog/item/a6eb970c98a644d67acbe15a.html
【2】 示例,该作者博文不错,涵盖了各个方面
http://hi.baidu.com/s_jqzhang/blog/item/a3c5df1f9408246ff624e4f5.html/cmtid/02e72e4fcb488039aec3ab28
【3】 boost库支持json比较好
http://freedomhui.com/?p=6
【3】 对json的类型作了简单的小结,为json-c进行了介绍
http://developer.51cto.com/art/201001/176060.htm
【4】 官网
http://jsoncpp.sourceforge.net/index.html
相关的类介绍及使用
http://jsoncpp.sourceforge.net/annotated.html
【5】 其他例子
http://joysofprogramming.com/json_parser_json-c/
http://forum.openframeworks.cc/index.php?topic=2833.0
相关文章推荐
- Json介绍及Jsoncpp简单使用(测试代码+测试实例)
- 5、JsonCpp简单使用(1)
- JsonCpp的简单使用方法
- jsoncpp 的学习和简单的使用案例
- JsonCpp的简单使用方法
- Jsoncpp的简单使用
- jsoncpp在VS2010下的简单使用方法 不需编译
- jsoncpp的简单使用
- jsoncpp开源库简单使用
- 6、JsonCpp简单使用(2)
- cocos2dx中Http链接以及使用JsonCPP解析的简单函数
- jsoncpp的简单使用方法
- json使用jsoncpp解析,简单方便
- JsonCpp 简单使用
- Jsoncpp的简单使用
- C++简单使用Jsoncpp来读取写入json文件
- C++ JsonCpp 使用(含源码下载)
- FastJSON 简单使用
- [rapidjson]_[C/C++]_[rapidjson库使用简单介绍]
- jqGrid简单使用、json格式和jsonReader介绍