例如:
Json::Reader reader;
Json::Value val;
ifstream file("JSON/test.json");
bool success = reader.parse(file, val, false);
vector<string> obj = val.getMemberNames();
for (int i = 0; i < val.size(); i++)
{
// switch type of value
switch (val.get(obj.at(i), "default").type())
{
case stringValue:
cout << "I'm string" << endl;
... need to save **membername** and **value**
break;
case intValue:
cout << "I'm int" << endl;
... need to save **membername** and **value**
break;
case nullValue:
cout << "I'm null" << endl;
break;
case arrayValue:
... …Run Code Online (Sandbox Code Playgroud) 我正在使用jsoncpp解析器(http://jsoncpp.sourceforge.net)来解析JSON数据.所以,如果我们有以下JSON:
{ "name": "Joseph", "age": 20 }
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得属性名称 的名称和值 约瑟夫,...岁和20后?好的,我们可以普遍做到这一点:
string e = root.get(propertyName, defaultValue).asString();
Run Code Online (Sandbox Code Playgroud)
把我们想要的真实:
string e = root.get(name, "Mark").asString();
Run Code Online (Sandbox Code Playgroud)
现在,变量e是约瑟夫,它有效.但我必须采取/写" 名字 ".我不想查询(没有质疑我想得到的功能"名称"(属性名称)和"约瑟夫"(属性值)).
之后最好存储在一个字段中(例如C/C++):
property[name][0] = "Joseph"
property[age][0] = 20
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?还是其他任何想法?
在C#中我们可以使用这样的东西:
MyClass c = new MyClass();
c.Properties.Add("another property", "another value");
Run Code Online (Sandbox Code Playgroud)
我需要在C++中这样做.我正在使用VS 2012.有什么想法吗?
编辑:知道,我可以使用地图或任何列表将属性保存到对象,如:
void ObjectProperty::addItem(string key, XProperty p)
{
_object[key] = p;
}
Run Code Online (Sandbox Code Playgroud)