我一直在尝试使用以下代码,取自示例,我不得不将 json_object_object_get(struct json_object *obj, const char *key) 更改为 json_object_get_ex(struct json_object *obj, const char *key, struct json_object * *价值)
很抱歉,我已经发布了类似的问题,因为我几天来一直试图找到一种从套接字解析 json 的方法,但我已经绝望了,但我做了更多的工作和研究,我认为这非常接近。我从编译以下得到的错误是:
server4.c:在函数'main'中:server4.c:62:错误:取消引用指向不完整类型的指针server4.c:68:警告:赋值使指针从整数而不进行强制转换
struct json_object *jobj, *val_jobj, *value;
char const *val;
char buf[50];
fgets(buf, sizeof(buf), stdin);
printf("Input JSON : %s", buf);
char const *val;
*jobj = json_tokener_parse(buf);
if (is_error(jobj))
return (-1);
printf("Received JSON in String format : %s\n",
json_object_to_json_string(jobj));
//Get the value for the key "name"
val_jobj = json_object_object_get_ex(jobj, "name", &value);
printf("Extracted value for command : %s\n",
//Get the string from the value of the key "name"
val = json_object_get_string(val_jobj);
printf("String value returned : %s\n", val);
我看不出有什么问题,也不完全理解 json-c 的工作原理,我也更熟悉 c++,当然我也在那里使用指针。无论哪种方式,通过阅读一些 c++ 的 json 解析器,我发现它们更容易理解。
提前致谢