这个问题是关于一个关于服务器相关通信和加密的项目中使用的两个小函数。到目前为止,我仅使用 C 进行编程,但由于项目的性质,我被指示通过 Parson 头文件 (parson.h) 使用 JavaScript。
Could someone please help me rid my code of the warnings in the two functions below? An elaboration will follow the code.
通常我能够解决这些类型的问题,但现在我不太确定问题出在哪里......
//Write a function to write a name/value pair to a given JSON value containing an object. The function prototype must be as follows:
void setNameValuePair(JSON_Value *rootValue, const char *name, const char *value) {
//This is of course the body of the function I wrote
json_object_set_string(rootValue, name, value);
}
//Write a function that reads the value associated with a specific name in a given JSON value containing an object. The function prototype must be as follows:
const char* getValueFromName(const JSON_Value *rootValue, const char *name) {
//And here is my function body again
const char *value;
value = json_object_dotget_string(rootValue, name);
return value;
}
在这两种情况下,从技术上讲我都没有收到错误,但有两个类似的警告(我相信存在差异),说明以下内容:
从不兼容的指针类型传递“json_object_set_string”的参数 1 [默认启用]
,
和
从不兼容的指针类型传递“json_object_dotget_string”的参数 1 [默认启用]
。
上面写着“默认启用”,但是在测试功能时,我的程序崩溃了。
我收到了一些评论,建议我查找从 parson.h 调用的函数并记下它们所需的参数 - 我发现了这一点,但我希望能帮助您理解它:
/* dotget functions enable addressing values with dot notation in nested objects,
just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
Because valid names in JSON can contain dots, some values may be inaccessible
this way. */
JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
const char * json_object_dotget_string (const JSON_Object *object, const char *name);
JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */
int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */
/* Creates new name-value pair or frees and replaces old value with a new one.
* json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */
JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
JSON_Status json_object_set_null(JSON_Object *object, const char *name);
我认为我无法扩展、解释或包含更多信息。你现在看到的就是我所拥有的……
我怀疑解决方案非常简单 - 补丁以及快速解释将不胜感激!
最佳答案
您应该注意文件parson.h
中的函数声明和注释。如果有些不清楚,您还可以查看文件 parson.c
及其实现。
您的函数接收指向 JSON_Value
的指针,但您尝试将此指针传递给接受指向 JSON_Object
的指针的函数。这些类型是不同的。仅仅将一个转换到另一个是不够的。
如果您确定提供的 JSON_Value
包含一个对象,则可以使用 json_value_get_object(root_value);
获取该对象。但是,如果 JSON_Value
不是对象(例如数组或字符串),则此函数返回 NULL
。因此,如果可能出现这种情况,就需要进行适当的处理。
所需功能:
void setNameValuePair(JSON_Value *rootValue, const char *name, const char *value)
{
/* NULL is returned if rootValue is not an object */
JSON_Object *root_object = json_value_get_object(root_value);
json_object_set_string(root_object, name, value);
}
const char* getValueFromName(const JSON_Value *rootValue, const char *name)
{
/* NULL is returned if rootValue is not an object */
JSON_Object *root_object = json_value_get_object(root_value);
/* NULL is returned if object field is not a string*/
return json_object_dotget_string(root_object, name);
}
您还应该小心内存分配和内存泄漏。例如,函数 json_object_dotget_string
返回的不是字符串的副本,而是返回指向用于创建该 JSON 的同一字符串的指针。因此,如果初始字符串被释放,getValueFromName
返回的字符串也会被释放。
关于c - 将名称/值对写入 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32847528/