2

I have this JSON:

[{"header": "test" , "test2" : "test2"}]

I'm trying to parse this using jsoncpp.

Here's my code snippet:

 Json::CharReaderBuilder builder;
 Json::CharReader *reader = builder.newCharReader();
 Json::Value root;
 bool parseRet = reader->parse(serverResponse.c_str(),serverResponse.c_str() +serverResponse.size(), &root, &errors);

parseRet returns true. But root does not include json data.

How do I parse this?

4
  • Did you check the errors? Commented Feb 14, 2020 at 6:28
  • @Azeem Yes, errors is empty string. Commented Feb 14, 2020 at 6:30
  • try without [] paranthesis? Commented Feb 14, 2020 at 6:33
  • @anas: Can you add your full code example with the errors you're getting? Commented Feb 14, 2020 at 6:48

1 Answer 1

1

parseRet returns true. But root does not include JSON data.

This sounds like an issue with the way you're accessing the parsed JSON elements.


Below is a complete working example with a raw string literal as JSON input.

A few points:

  • Use std::unique_ptr for reader to de-allocate memory appropriately at the end. In your code snippet, it's a memory leak.
  • Read the documentation carefully! Accessing an element using a method or operator might return a default value or an exception so handle accordingly. For example, the JSON in root is an array so it should be accessed by index and then each index contains an object i.e. root[0]["header"].

Example (C++11):

#include <iostream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>

int main()
{
    const std::string raw_json = R"json([{"header": "test" , "test2" : "test2"}])json";

    Json::CharReaderBuilder builder {};

    // Don't leak memory! Use std::unique_ptr!
    auto reader = std::unique_ptr<Json::CharReader>( builder.newCharReader() );

    Json::Value root {};
    std::string errors {};
    const auto is_parsed = reader->parse( raw_json.c_str(),
                                          raw_json.c_str() + raw_json.length(),
                                          &root,
                                          &errors );
    if ( !is_parsed )
    {
        std::cerr << "ERROR: Could not parse! " << errors << '\n';
        return -1;
    }

    std::cout << "Parsed JSON:\n" << root << "\n\n";

    try
    {
        std::cout << "header: " << root[0]["header"] << '\n';
        std::cout << "test2 : " << root[0]["test2"] << '\n';
    }
    catch ( const Json::Exception& e )
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}

Output:

Parsed JSON:
[
    {
        "header" : "test",
        "test2" : "test2"
    }
]

header: "test"
test2 : "test2"
Sign up to request clarification or add additional context in comments.

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.