3

我有以下代码

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

我知道我们必须用来json_tokener_parse()解析 json 字符串,但是我不知道如何从 json_object 中提取值,new_obj如上面代码中的注释所示

之后如何获取json值 json_tokener_parse()

4

3 回答 3

7

首先,您需要获取json_object特定节点:

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

...然后您可以使用适当的 getter 来获取特定类型的节点的值:

char *foo_val = json_object_get_string(obj_foo2);

所以,简而言之,你可以这样做:

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

显然,最好分多个步骤进行,这样您就可以检查错误(在这种情况下:空指针)并防止未定义的行为等。

您可以在此处找到 JSON C API 文档

于 2013-02-14T17:19:40.453 回答
3

接受的答案显示了如何使用json_object_object_get现在已弃用的功能。

json_object_object_get_ex应改为使用。

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
}
于 2018-01-21T02:58:20.873 回答
0

您好,请查看此示例(已测试)。复制并粘贴到您的 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


}
于 2017-01-10T10:55:30.693 回答