C 将名称/值对写入JSON对象
这个问题是关于服务器相关通信和加密项目中使用的两个小函数。到目前为止,我只使用C语言编程,但由于项目的性质,我被指示通过Parson头文件(Parson.h)使用JavaScript 有人能帮我去掉这两个警告中的代码吗 下面的功能?随后将进行详细说明 通常我可以解决这类问题,但现在我不确定问题出在哪里C 将名称/值对写入JSON对象,c,json,C,Json,这个问题是关于服务器相关通信和加密项目中使用的两个小函数。到目前为止,我只使用C语言编程,但由于项目的性质,我被指示通过Parson头文件(Parson.h)使用JavaScript 有人能帮我去掉这两个警告中的代码吗 下面的功能?随后将进行详细说明 通常我可以解决这类问题,但现在我不确定问题出在哪里 //Write a function to write a name/value pair to a given JSON value containing an object. The func
//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\u object\u set\u string”的参数1
及
从不兼容的指针类型[默认启用]传递“json\u object\u dotget\u 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\u值的指针,但您尝试将此指针传递给接受指向JSON\u对象的指针的函数。这些类型是不同的。仅仅把一个抛给另一个是不够的
如果您确实知道提供的JSON\u值
包含一个对象,则可以使用JSON\u值\u get\u对象(root\u值)获取该对象代码>。但是,如果JSON\u 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\u object\u dotget\u string
不返回字符串的副本,但返回指向用于创建该json的同一字符串的指针。因此,如果初始字符串被释放,那么由getValueFromName
返回的字符串也会被释放。我们不知道函数json\u object\uuuu…
的作用是什么!您似乎试图在常量缓冲区上设置一个值!此外,您确定资源已分配给要修改的缓冲区吗?您调用的json\u object.*
不正确。这是我们能说的最多的信息量。张贴您收到的文字错误文本。还要阅读parsons.h
中的函数声明。在Parson.h中找到json函数,看看它们对参数的期望值。发布准确的错误消息,有时是有你所需要的。好的-谢谢大家。我已经看了你说的话并编辑了我的问题。请看看这是否能让你更好地理解我应该做什么……太棒了,谢谢!这不仅使我的函数通过了测试,而且我对我正在做的事情有了更好的理解。谢谢你的帮助!