如何使用MongoDB自带的json库来反序列json字符串
2012-03-23 17:46
435 查看
需求:
在对mongodb中的字段值进行解析的时候发现,因为这个值是json字符串,需要对其进行反序列化。解决方法:
首先想到了到http://www.json.org/json-zh.html网站去找相应的C++库,试了一下jsoncpp和JSON Spirit,因为是用scons来构建了,装了一下,编译以后玩不起来,放弃了。再试JSON Spirit,(http://www.codeproject.com/Articles/20027/JSON-Spirit-A-C-JSON-Parser-Generator-Implemented), 这东东不错,好像是依赖于Boost Spirit的,这个我也装了,安装过程还是满方便的,因为我的C++项目都是用CMake的,这个也是用它的,所以编译安装没有遇到什么问题。下面给出一个相应的例子:#include <json_spirit.h> const std::string test = "{" " \"text\": \"Home-plate umpire Crawford gets stung http://tinyurl.com/27ujc86\"," " \"favorited\": false," " \"source\": \"<a href=\\\"http://apiwiki.twitter.com/\\\" rel=\\\"nofollow\\\">API</a>\"," " \"user\": {" " \"name\": \"Johnathan Thomas\"" " }" "}"; int main() { namespace js = json_spirit; js::mValue top; js::read(std::string(test), top); json_spirit::mObject obj = top.get_obj(); std::cout << "--------" << std::endl; std::cout << obj["text" ].get_str() << "\n" << obj["favorited"].get_bool() << "\n" << obj["source" ].get_str() << "\n" << obj["user" ].get_obj()["name"].get_str() << "\n"; }
下面想到了mongodb有自己的bson结构,这个东东和json是差不多的,在mongodb的源代码包里找到了json.h这个文件看,盾到了fromjson这个方法,看来能行。呵呵
下面是我的测试代码,因为我的value中用到了array,这里还用到了别外一个把BSONObj转换成Array<BSONObj>的方法。
#include <db/json.h> // load fromjson method #include <iostream> #include <string> const std::string test = "{ \"specs\" : " " [ {\"id\":\"value1\" , \"name\":\"jack\"}," " {\"id\": \"value2\", \"name\":\"jack2\"}," "{\"id\": \"value3\", \"name\":\"jack3\"}" " ]" "}"; int main() { try{ // { "specs" : [ {"id":"value1" , "name":"jack"}, {"id": "value2", "name":"jack2"},{"id": "value3", "name":"jack3"} ]} std::cout << "Test json string:" << test << std::endl; // parse json method from json.h file // throws MsgAssertionException if parsing fails. The message included with // this assertion includes a rough indication of where parsing failed. mongo::BSONObj obj = mongo::fromjson(test); mongo::BSONObj eles = obj["specs"].Obj(); // get array obj /** add all values of the object to the specified vector. If type mismatches, exception. this is most useful when the BSONObj is an array, but can be used with non-arrays too in theory. example: bo sub = y["subobj"].Obj(); vector<int> myints; sub.Vals(myints); */ vector<mongo::BSONObj> specs; eles.Vals(specs); // print values for(int i = 0; i < 3; ++i) std::cout << specs.at(i)["id"].String() << ":" << specs.at(i)["name"].String()<< endl; } catch(const mongo::MsgAssertionException& e) { std::cout << "parse exception " << e.what() << endl; } }
运行结果:
gxl@gxl-desktop:~/Test_place$ g++ sample.cpp -o sample -I/usr/local/include/mongo/ -lmongoclient gxl@gxl-desktop:~/Test_place$ ./sample Test json string:{ "specs" : [ {"id":"value1" , "name":"jack"}, {"id": "value2", "name":"jack2"},{"id": "value3", "name":"jack3"} ]} value1:jack value2:jack2 value3:jack3
相关文章推荐
- 如何使用MongoDB自带的json库来反序列json字符串
- 如何使用EditPlus将json格式字符串默认为UTF-8格式
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- MongoDB 自带JSON类 序列化自定义对象为Json字符串解决方案
- 教你如何使用PHP输出中文JSON字符串
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- 如何将一个json字符串当成一个JSON对应来使用,在java中
- 如何使用Retrofit获取服务器返回来的JSON字符串
- 如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能
- 如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- 如何在一个已排序的NSArray中搜索某一特定字符串?答案是使用CFArray自带的搜索功能:
- 教你如何使用PHP输出中文JSON字符串
- 如何使用ajax接受json返回的字符串数组,并动态异步显示到超链接的下面
- Android项目中如果json字符串中key-value顺序很重要,不要使用Android自带的JSONObject
- .net如何序列换对象为Json字符串
- 如何在前台脚本通过json传递数据到后台(使用微软自带的ajax)
- 如何使得中文不被 json_encode 不编码成 unicode PHP 浏览:9722 2014年08月14日 使用 php 自带的 json_encode 函数对数据进行编码时,中文都会变成
- 如何使用EditPlus将json格式字符串默认为UTF-8格式
- 如何使用ABAP代码反序列化JSON字符串成ABAP结构