3

i'm trying to use JsonCpp with this sample code

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;

but i've received this error on this "..array[index].asString..."

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

can anyone help me?!tnx so much

1
  • 1
    The code above compiles for me. Are you getting the link error ? Something like LNK2019 in Visual Studio ? Add the reference to the precompiled jsoncpp library to your project. Commented Sep 14, 2012 at 18:40

2 Answers 2

1

Tnx for your answer... So the solution is changing this

 for (int index = 0; index < array.size(); ++index) {   // Iterates over the sequence elements.
    cout << "Element " << index << " in array: " << array[index].asString() << endl;
}

with

 for (unsigned int index = 0; index < array.size(); ++index) {   // Iterates over the sequence elements.
    cout << "Element " << index << " in array: " << array[index].asString() << endl;
}

bye

Sign up to request clarification or add additional context in comments.

1 Comment

This is a quirk of an unfortunate API decision. Sorry.
0

I think you didn't included jsoncpp.cpp file, which describes your json.h library file. U have to generate it using Pythot with command python amalgamate.py and then include it into your project

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.