How can I add many json strings in one JsonArray?

996 Views Asked by At

I want one JSonArray as the output.I want json strings to be added in that array. I am using json-glib and C++ for this. I have damn searched a lot on Google but all in vain. I want something like this:

[ {"id": "i_am_id", "client":"i_am_C" ,"tag": [{"one": "value", "two": "value"}] } ,{} ,{} ]

"tag" is a jsonarray of strings as written. I want a jsonarray of many json like this in which there are fields like string, int, jsonarrays. Please help me with this.

1

There are 1 best solutions below

7
On BEST ANSWER

I'm not using json-glib,
I couldn't test this code,
and I omitted the part to release the builders,
but I hope this helps you:

// build an object as an element of 'tag' array.
// {"one": "value", "two": "value"}
JsonNode * objectAsElementOfTag;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_object (builder);

  json_builder_set_member_name (builder, "one");
  json_builder_add_string_value (builder, "value");

  json_builder_set_member_name (builder, "two");
  json_builder_add_string_value (builder, "value");

  json_builder_end_object (builder);
  objectAsElementOfTag = json_builder_get_root (builder);
}

// build 'tag' array.
// [{"one": "value", "two": "value"}]
JsonNode * tagArray;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_array (builder);

  json_builder_add_value (builder, objectAsElementOfTag);

  json_builder_end_array (builder);
  tagArray = json_builder_get_root (builder);
}

// build the first element.
// {"id": "i_am_id", "client":"i_am_C" ,"tag": [{"one": "value", "two": "value"}] }
JsonNode * firstElement;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_object (builder);

  json_builder_set_member_name (builder, "id");
  json_builder_add_string_value (builder, "i_am_id");

  json_builder_set_member_name (builder, "client");
  json_builder_add_string_value (builder, "i_am_C");

  json_builder_set_member_name (builder, "tag");
  json_builder_add_value (builder, tagArray);

  json_builder_end_object (builder);
  firstElement = json_builder_get_root (builder);
}

// build the second element(an empty object).
// {}
JsonNode * secondElement;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_object (builder);

  json_builder_end_object (builder);
  secondElement = json_builder_get_root (builder);
}

// build the third element(an empty object).
// {}
JsonNode * thirdElement;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_object (builder);

  json_builder_end_object (builder);
  thirdElement = json_builder_get_root (builder);
}

// finally, build the root array which may be what you want.
// [ {"id": "i_am_id", "client":"i_am_C" ,"tag": [{"one": "value", "two": "value"}] } ,{} ,{} ]
JsonNode * rootArray;
{
  JsonBuilder *builder = json_builder_new ();
  json_builder_begin_array (builder);

  json_builder_add_value (builder, firstElement);
  json_builder_add_value (builder, secondElement);
  json_builder_add_value (builder, thirdElement);

  json_builder_end_array (builder);
  rootArray = json_builder_get_root (builder);
}