0

I am using jsonc-libjson to create a json string like below.

{ "author-details": {
        "name" : "Joys of Programming",
        "Number of Posts" : 10
    }
}

My code looks like below

json_object *jobj = json_object_new_object();
json_object *jStr1 = json_object_new_string("Joys of Programming");
json_object *jstr2 = json_object_new_int("10");
json_object_object_add(jobj,"name", jStr1 );
json_object_object_add(jobj,"Number of Posts", jstr2 );

this gives me json string

{
 "name" : "Joys of Programming",
    "Number of Posts" : 10
}

How do I add the top part associated with author details?

3 Answers 3

1

To paraphrase an old advertisement, "libjson users would rather fight than switch."

At least I assume you must like fighting with the library. Using nlohmann's JSON library, you could use code like this:

nlohmann::json j {
    { "author-details", {
            { "name", "Joys of Programming" },
            { "Number of Posts", 10 }
        }
    } 
};

At least to me, this seems somewhat simpler and more readable.

Parsing is about equally straightforward. For example, let's assume we had a file named somefile.json that contained the JSON data shown above. To read and parse it, we could do something like this:

nlohmann::json j;

std::ifstream in("somefile.json");

in >> j;   // Read the file and parse it into a json object

// Let's start by retrieving and printing the name.
std::cout << j["author-details"]["name"];

Or, let's assume we found a post, so we want to increment the count of posts. This is one place that things get...less tasteful--we can't increment the value as directly as we'd like; we have to obtain the value, add one, then assign the result (like we would in lesser languages that lack ++):

j["author-details"]["Number of Posts"] = j["author-details"]["Number of Posts"] + 1;

Then we want to write out the result. If we want it "dense" (e.g., we're going to transmit it over a network for some other machine to read it) we can just use <<:

somestream << j;

On the other hand, we might want to pretty-print it so a person can read it more easily. The library respects the width we set with setw, so to have it print out indented with 4-column tab stops, we can do:

somestream << std::setw(4) << j;
Sign up to request clarification or add additional context in comments.

2 Comments

I second the motion to use nlohmann's library. I've used it before without too much trouble and with success.
Thanks all for recommending nlohmann's JSON. I will try that out. To jump start, If someone can throw snippet of how to parse json data/object like above using the nlohman's json library that would be great.
0

Create a new JSON object and add the one you already created as a child.

Just insert code like this after what you've already written:

json_object* root = json_object_new_object();
json_object_object_add(root, "author-details", jobj); // This is the same "jobj" as original code snippet.

1 Comment

It doesn't produce the desired results. json_object jobj = json_object_new_object(); json_object root = json_object_new_object (); json_object_object_add(jobj, "author-details", root); I added this before the code I provided earlier. json_object *jobj = json_object_new_object(); json_object *jStr1 = json_object_new_string("Joys of Programming"); json_object *jstr2 = json_object_new_int(10); It prints { "author-details": { }, "name": "Joys of Programming", "Number of Posts": 10 }
0

Based on the comment from Dominic, I was able to figure out the correct answer.

json_object *jobj = json_object_new_object();
json_object* root = json_object_new_object();
json_object_object_add(jobj, "author-details", root);
json_object *jStr1 = json_object_new_string("Joys of Programming");
json_object *jstr2 = json_object_new_int(10);
json_object_object_add(root,"name", jStr1 );
json_object_object_add(root,"Number of Posts", jstr2 );

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.