3

このサンプル コードで 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そんなに

4

2 に答える 2

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

さよなら

于 2012-09-15T14:42:40.490 に答える