5

I have a sample file "sample.json" which contains 3 json objects

{"A":"something1","B":"something2","C":"something3","D":"something4"}{"A":"something5","B":"something6","C":"something7","D":"something8"}{"A":"something9","B":"something10","C":"something11","D":"something12"}

(there is no newline in above file)

I want to read all three json objects using jsoncpp.

I'm able to read the first object but not after it.

Here is relevant portion of my code

    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    std::ifstream test("sample.json", std::ifstream::binary);
    bool parsingSuccessful = reader.parse(test, root, false);
    int N = 3;
    if (parsingSuccessful)
    {
         for (size_t i = 0; i < N; i++)
         {
                std::string A= root.get("A", "ASCII").asString();
                std::string B= root.get("B", "ASCII").asString();
                std::string C= root.get("C", "ASCII").asString();
                std::string D= root.get("D", "ASCII").asString();
               //print all of them
        }
    }

2 Answers 2

6

I believe your JSON file is syntactically invalid. See www.json.org. Your file should contain a single object or array, e.g. in your case it should like this:

[{"A":"something1","B":"something2","C":"something3","D":"something4"},
 {"A":"something5","B":"something6","C":"something7","D":"something8"}, 
 {"A":"something9","B":"something10","C":"something11","D":"something12"}]

Then you can access each object of the array in your loop:

for (Json::Value::ArrayIndex i = 0; i != root.size(); i++)
{
    std::string A = root[i].get("A", "ASCII").asString();
    // etc.
}
Sign up to request clarification or add additional context in comments.

3 Comments

It looks like the index should be of type for (Json::Value::ArrayIndex i = 0; i != root.size(); i++)
JsonParser.cpp:125:27: error: ambiguous overload for ‘operator[]’ (operand types are ‘Json::Value’ and ‘size_t {aka long unsigned int}’) std::string A = root[i].get("A", "ASCII").asString();
@PPTim thanks, edited the answer. I just copy/pasted the code from the question but I guess it depends on the actual json parser used whether the overload works with size_t.
2

Here is a solution to the question, pretending there are newlines between each object (and no line is blank or malformed):

// Very simple jsoncpp test
#include <json/json.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    Json::Value root;
    Json::Reader reader;
    ifstream test("sample.json", ifstream::binary);
    string cur_line;
    bool success;

    do {
        getline(test, cur_line);
        cout << "Parse line: " << cur_line;
        success = reader.parse(cur_line, root, false);
        cout << root << endl;
    } while (success);

    cout << "Done" << endl;
}

2 Comments

Doesn't work gives me one parenthesis "{" and that's it.
Parse line: [{[ {} ] Done

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.