我有以下代码
#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_tokener_parse()
之后获取json值?
答案 0 :(得分: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"))
);
显然,最好在多个步骤中执行此操作,以便您可以检查错误(在这种情况下:空指针)并防止未定义的行为等。
答案 1 :(得分:0)
您好请参阅此示例(TESTED)。复制并粘贴到您的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
}
答案 2 :(得分:0)
接受的答案显示了如何使用现已弃用的for [Counter1] = 1 to [n]
for [Counter2] = [Counter1}+1 to [n]
Current combination is: ([Counter1], [Counter2])
next [Counter2]
next [Counter1]
函数。
json_object_object_get
。
json_object_object_get_ex