2012-09-14 55 views
2

我想要使用JsonCpp與此示例代碼JsonCpp:未定義參考`的Json ::值::運算符[](INT)常量」

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 [指數] .asString ......」

Undefined reference to `Json::Value::operator[](int) const' 

誰能幫助我?!TNX這麼多

+1

上面的代碼爲我編譯。你是否收到鏈接錯誤?像Visual Studio中的LNK2019?將對預編譯的jsoncpp庫的引用添加到項目中。 –

回答

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; 
} 

再見

+0

[這是一個怪癖](http://open-source-parsers.github.io/jsoncpp-docs/doxygen/class_json_1_1_value.html#af9e02b38f4e63e491c300c20b275bdd7)的一個不幸的API決定。抱歉。 – cdunn2001

0

我想你沒有包括jsoncpp.cpp文件,該文件描述了你的json.h庫文件。你必須使用命令python amalgamate.py使用Pythot生成它,然後將它包含到你的項目中