我想创建一个包含类的实例变量的JSON字符串.
例如
class Example {
std::string string;
std::map<std::string, std:string> map;
std::vector<int> vector;
};
将成为:
{
"string":"the-string-value",
"map": {
"key1":"val1",
"key2":"val2"
},
"vector":[1,2,3,4]
}
I've looked into several C++ libraries for creating JSON and they all seem incredibly complex. I'd like something similar to Javascript's JSON.stringify(object)
. In other words just pass a std::map to it and receive a string. The map could contain other maps, vectors, lists, strings, numbers and bools.
最好的方法是什么?
Thanks for your help.
Edit
我已经调查了以下几点:
json spirit, jsoncpp, zoolib, JOST, CAJUN, libjson, nosjob, JsonBox, jsonme--
据我所知,我可以构造一个单独的JSON对象,如下面的答案所示,然后转换为JSON.我希望能够将我的内容存储在标准集合中并进行转换.
Edit 2
Okay, scrap the idea of serializing a class since it appears that's impossible with C++'s lack of reflection.
有没有一种很好的方法可以将包含std:map、std::Vector、std::list、number、string和bools的std::map转换为JSON,而不必更改数据类型或将数据复制到新的数据类型?
谢谢