将cjson json转换为字符串
我正在使用将cjson json转换为字符串,c,cjson,C,Cjson,我正在使用cjson库创建json,并将其发送到服务器。我正在使用以下代码 char *string = cJSON_Print(json); 我得到如下输出 { "type1": "car", "type2": "bus", } 但是服务器希望它是这样的字符串格式 "{\n\t\"type1\":\t\"car\",\n\t\"type2\":\t\"bus\"\n}" 我很困惑如何实现这种转换?您确定服务器期望的不是json的C表示吗?或者可能是编码太多…请尝试cJSON\u
cjson
库创建json
,并将其发送到服务器。我正在使用以下代码
char *string = cJSON_Print(json);
我得到如下输出
{
"type1": "car",
"type2": "bus",
}
但是服务器希望它是这样的字符串格式
"{\n\t\"type1\":\t\"car\",\n\t\"type2\":\t\"bus\"\n}"
我很困惑如何实现这种转换?您确定服务器期望的不是json的C表示吗?或者可能是编码太多…请尝试
cJSON\u printformated
。“cJSON\u Print
将使用空格进行格式化打印。如果您想不格式化打印,请使用cJSON\u printformated
”这是来自cJSON read me的。cJSON\u Print将为您提供“{\n\t\“type1\”:\t\“car\”,\n\t\“type2\:\t\“bus\”\n}
。在stdout上打印时,它应该看起来像您实际获得的输出。那么,您已经做到了。您确定服务器期望的不是json的C表示吗?或者可能是编码太多…请尝试cJSON\u printformated
。“cJSON\u Print
将使用空格进行格式化打印。如果您想不格式化打印,请使用cJSON\u printformated
”这是来自cJSON read me的。cJSON\u Print将为您提供“{\n\t\“type1\”:\t\“car\”,\n\t\“type2\:\t\“bus\”\n}
。在stdout上打印时,它应该看起来像您实际获得的输出。所以,你已经做到了。