4

I have the following code

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include <json/json.h>

int main(int argc, char **argv)
{
      json_object *new_obj;
      char buf[] = "{ \"foo\": \"bar\", \"foo2\": \"bar2\", \"foo3\": \"bar3\" }"
      new_obj = json_tokener_parse(buf);
      printf("The value of foo is %s" /*What I have to put here?*/);
      printf("The value of foo2 is %s" /*What I have to put here?*/);
      printf("The value of foo3 is %s" /*What I have to put here?*/);
      json_object_put(new_obj);
}

I knwo that we have to use json_tokener_parse() to parse json strings but then I do not know how to extract values from the json_object new_obj as indicated in the comments in the code above

How to get json values after json_tokener_parse() ?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

3 Answers3

8

First you need to get the json_object to a specific node:

json_object *obj_foo = json_object_object_get(new_obj, "foo");

...then you can use the appropriate getter to obtain the value of the node in a specific type:

char *foo_val = json_object_get_string(obj_foo2);

So, in short, you could do:

printf("The value of foo is %s", 
    json_object_get_string(json_object_object_get(new_obj, "foo"))
);

Obviously, it's better to do it in multiple steps so that you can check for errors (in this case: null pointers) and prevent undefined behavior and such.

You can find the JSON C API documentation here.

Andy Alt
  • 297
  • 2
  • 12
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • the object returned returned by `json_object_object_get(new_obj, "foo")` should be free with `json_object_put()`. isn't? – MOHAMED Feb 14 '13 at 17:28
  • and the string returnd by `json_object_get_string()` should be free with `free()` function. isn't? – MOHAMED Feb 14 '13 at 17:29
  • @MohamedKALLEL: No, they don't allocate, they both return pointers to the original object , which is freed when you `json_object_put` at the end. In fact, if you store those returned pointers somewhere, `json_object_put` and *then* try to access them, they'll be dangling and you'll get undefined behavior. – netcoder Feb 14 '13 at 17:31
  • So I do not have to be worry concerning memory (even for string) all will be removed at the end with `json_object_put()`. isn't ? – MOHAMED Feb 14 '13 at 17:35
3

The accepted answer shows how to use json_object_object_get function which is now deprecated.

json_object_object_get_ex should be used instead.

const char *json = "{ \"Name\": \"xxxxx\", \"Id\": 101, \"Voting_eligible\": true }";
json_object *root_obj = json_tokener_parse(json);

json_object *tmp;
if (json_object_object_get_ex(root_obj, "Name", &tmp){
    // Key Name exists
    printf("Name: %s\n", json_object_get_string(tmp));
    // Name: xxxxx
}
Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
0

Hi pls see this sample (TESTED). copy and paste into ur IDE

#include <stdio.h>
#include <json/json.h>

int main()
{
  /*Declaring the json data's in json format*/
  char buf[] = "{ \"Name\": \"xxxxx\", \"Id\": 101, \"Voting_eligible\": true }";

  /*Declaring the Json_object.To pass the Json string to the newly created Json_object*/
  json_object *new_obj = json_tokener_parse(buf);

  /*To get the data's then we have to get to the specific node by using the below function*/

  json_object *obj_Name;//Declaring the object to get  store the value of the Name
  obj_Name = json_object_object_get(new_obj,"Name");//This in-built func used to traverse through specific node

  json_object *obj_Id;//Declaring the object to get  store the value of the  Id
  obj_Id = json_object_object_get(new_obj,"Id");//This in-built func used to traverse through specific node

  json_object *obj_Vote;//Declaring the object to get  store the value of the  Vote
  obj_Vote = json_object_object_get(new_obj,"Voting_eligible");//This in-built func used to traverse through specific node

  /* To store the values we use temp char */
  char *Name = json_object_get_string(obj_Name);// This is in-built func to get the string(value) from "json_object_object_get()"
  char *Id = json_object_get_string(obj_Id);
  char *Vote = json_object_get_string(obj_Vote);




  /* we can also use like this statement directly to reduce the pgm size */
  //  printf("Name : %s\n",json_object_get_string(json_object_object_get(new_obj, "Name")));
  //  printf("Id : %s\n",json_object_get_string(json_object_object_get(new_obj, "Id")));
  //  printf("Voting_eligible : %s\n",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

  printf("Name : %s\n",Name);
  printf("Id : %s\n",Id);
  printf("Voting_eligible : %s\n",Vote);

  json_object_put(new_obj);// to return the pointer to its originalobjects


}
Abdulvakaf K
  • 606
  • 1
  • 7
  • 16