ari
ari

Reputation: 21

Creating json string using json lib

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?

Upvotes: 0

Views: 2034

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490808

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;

Upvotes: 1

ari
ari

Reputation: 21

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

Upvotes: 0

Dominic Dos Santos
Dominic Dos Santos

Reputation: 2713

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.

Upvotes: 0

Related Questions