我正在尝试将JsonCpp与此示例代码
一起使用string json_example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
// Let's parse it
Json::Value root;
Json::Reader reader;
//Parsing of something. In example do parsing of json_example string.
bool parsedSuccess = reader.parse(json_example, root, false);
if (!parsedSuccess) {
// report to the user the failure and their locations in the document.
cout << "Failed to parse JSON" << endl << reader.getFormatedErrorMessages() << endl;
return 1;
}
// Let's extract the array that is contained inside the root object
const Json::Value array = root["array"];
// And print its values
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
// Lets extract the not array element contained in the root object and print its value
cout << "Not an array: " << root["not an array"] << endl;
// If we want to print JSON is as easy as doing:
cout << "Json Example pretty print: " << endl << root.toStyledString() << endl;
return 0;
但是我在这个“..array [index] .asString ...”
上收到了这个错误Undefined reference to `Json::Value::operator[](int) const'
任何人都可以帮助我吗?!tnx这么多
答案 0 :(得分:1)
Tnx的答案...... 所以解决方案正在改变这个
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
与
for (unsigned int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
再见
答案 1 :(得分:0)
我认为您没有包含jsoncpp.cpp文件,该文件描述了您的json.h库文件。你必须用命令python amalgamate.py
使用Pythot生成它,然后将它包含在你的项目中