How to get json values after json_tokener_parse()?












2















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() ?










share|improve this question



























    2















    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() ?










    share|improve this question

























      2












      2








      2








      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() ?










      share|improve this question














      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() ?







      c json libjson






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 14 '13 at 16:48









      MOHAMEDMOHAMED

      20.7k35112196




      20.7k35112196
























          3 Answers
          3






          active

          oldest

          votes


















          7














          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.






          share|improve this answer


























          • 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











          • @MohamedKALLEL: Exactly.

            – netcoder
            Feb 14 '13 at 17:36



















          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 : %sn",json_object_get_string(json_object_object_get(new_obj, "Name")));
          // printf("Id : %sn",json_object_get_string(json_object_object_get(new_obj, "Id")));
          // printf("Voting_eligible : %sn",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

          printf("Name : %sn",Name);
          printf("Id : %sn",Id);
          printf("Voting_eligible : %sn",Vote);

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


          }





          share|improve this answer

































            0














            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: %sn", json_object_get_string(tmp));
            // Name: xxxxx
            }





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14879876%2fhow-to-get-json-values-after-json-tokener-parse%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              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.






              share|improve this answer


























              • 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











              • @MohamedKALLEL: Exactly.

                – netcoder
                Feb 14 '13 at 17:36
















              7














              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.






              share|improve this answer


























              • 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











              • @MohamedKALLEL: Exactly.

                – netcoder
                Feb 14 '13 at 17:36














              7












              7








              7







              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.






              share|improve this answer















              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 7:30









              andy5995

              5610




              5610










              answered Feb 14 '13 at 17:19









              netcodernetcoder

              54k14104132




              54k14104132













              • 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











              • @MohamedKALLEL: Exactly.

                – netcoder
                Feb 14 '13 at 17:36



















              • 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











              • @MohamedKALLEL: Exactly.

                – netcoder
                Feb 14 '13 at 17:36

















              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





              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





              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







              @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





              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













              @MohamedKALLEL: Exactly.

              – netcoder
              Feb 14 '13 at 17:36





              @MohamedKALLEL: Exactly.

              – netcoder
              Feb 14 '13 at 17:36













              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 : %sn",json_object_get_string(json_object_object_get(new_obj, "Name")));
              // printf("Id : %sn",json_object_get_string(json_object_object_get(new_obj, "Id")));
              // printf("Voting_eligible : %sn",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

              printf("Name : %sn",Name);
              printf("Id : %sn",Id);
              printf("Voting_eligible : %sn",Vote);

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


              }





              share|improve this answer






























                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 : %sn",json_object_get_string(json_object_object_get(new_obj, "Name")));
                // printf("Id : %sn",json_object_get_string(json_object_object_get(new_obj, "Id")));
                // printf("Voting_eligible : %sn",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

                printf("Name : %sn",Name);
                printf("Id : %sn",Id);
                printf("Voting_eligible : %sn",Vote);

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


                }





                share|improve this answer




























                  0












                  0








                  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 : %sn",json_object_get_string(json_object_object_get(new_obj, "Name")));
                  // printf("Id : %sn",json_object_get_string(json_object_object_get(new_obj, "Id")));
                  // printf("Voting_eligible : %sn",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

                  printf("Name : %sn",Name);
                  printf("Id : %sn",Id);
                  printf("Voting_eligible : %sn",Vote);

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


                  }





                  share|improve this answer















                  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 : %sn",json_object_get_string(json_object_object_get(new_obj, "Name")));
                  // printf("Id : %sn",json_object_get_string(json_object_object_get(new_obj, "Id")));
                  // printf("Voting_eligible : %sn",json_object_get_string(json_object_object_get(new_obj, "Voting_eligible")));

                  printf("Name : %sn",Name);
                  printf("Id : %sn",Id);
                  printf("Voting_eligible : %sn",Vote);

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


                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  answered Jan 10 '17 at 10:55


























                  community wiki





                  Abdulvakaf K
























                      0














                      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: %sn", json_object_get_string(tmp));
                      // Name: xxxxx
                      }





                      share|improve this answer






























                        0














                        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: %sn", json_object_get_string(tmp));
                        // Name: xxxxx
                        }





                        share|improve this answer




























                          0












                          0








                          0







                          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: %sn", json_object_get_string(tmp));
                          // Name: xxxxx
                          }





                          share|improve this answer















                          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: %sn", json_object_get_string(tmp));
                          // Name: xxxxx
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 8 '18 at 5:59

























                          answered Jan 21 '18 at 2:58









                          Francesco LauritaFrancesco Laurita

                          20.5k74862




                          20.5k74862






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14879876%2fhow-to-get-json-values-after-json-tokener-parse%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Costa Masnaga