【问题标题】:How to get json values after json_tokener_parse()?如何在 json_tokener_parse() 之后获取 json 值?
【发布时间】:2013-02-14 16:48:13
【问题描述】:

我有以下代码

#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 中提取值,如上面代码中的 cmets 所示

json_tokener_parse() 之后如何获取 json 值?

【问题讨论】:

    标签: c json libjson


    【解决方案1】:

    首先您需要将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"))
    );
    

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

    你可以find the JSON C API documentation here

    【讨论】:

    • json_object_object_get(new_obj, "foo") 返回的对象应该是免费的 json_object_put()。不是吗?
    • json_object_get_string() 返回的字符串应该是免费的free() 函数。不是吗?
    • @MohamedKALLEL:不,它们不分配,它们都返回指向原始对象的指针,当您最后json_object_put 时释放该指针。事实上,如果您将这些返回的指针存储在某处,json_object_put然后 尝试访问它们,它们将悬空并且您将获得未定义的行为。
    • 所以我不必担心内存(即使是字符串),最后都会用json_object_put() 删除。不是吗?
    【解决方案2】:

    接受的答案显示了如何使用现在已弃用的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
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-14
        • 2014-07-21
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 2015-11-03
        • 1970-01-01
        • 2012-12-05
        相关资源
        最近更新 更多