How to get json values after json_tokener_parse()?

妖精的绣舞 提交于 2021-02-07 19:51:33

问题


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


回答1:


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.




回答2:


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
}



回答3:


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


}


来源:https://stackoverflow.com/questions/14879876/how-to-get-json-values-after-json-tokener-parse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!