我正在尝试从 ESP32 上的 http 服务器接收 json 数据,但是每次尝试访问 json 中的数据时,我都会感到核心恐慌。POST 请求是使用简单的 html 表单完成的:
<form method="post" enctype='text/plain'>
<fieldset>
<legend>Fermentation:</legend>
<input name="type" type="radio" value="Ale" /> Ale <br />
<input checked="checked" name="type" type="radio" value="Lager" /> Lager <br />
<label for="primary-duration">Duration of primary</label><br />
<input name="primary-duration" type="text" value="5" /> <br />
<input type="submit" value="Submit"><br />
</fieldset>
</form>
char *buf
包含来自 POST 的数据,如下所示:type=Lager primary-duration=5\0
将数据读入 buf 后,我正在使用 cJSON 解析它
cJSON *root = cJSON_Parse(buf);
并提取“类型”对象
const cJSON *typeJSON = cJSON_GetObjectItemCaseSensitive(root, "type");
获取我的 cJSON 对象后,_IsString() 将其正确识别为字符串,但在尝试访问它时出现“LoadProhibited”恐慌。
if (cJSON_IsString(typeJSON) && (typeJSON->valuestring != NULL))
{
printf("%s", typeJSON->valuestring);
}else if(cJSON_IsString(typeJSON))
{
ESP_LOGE(SERVER_HANDLER_TAG, "String error: Not a string");
}else
{
ESP_LOGE(SERVER_HANDLER_TAG, "String error: empty string"); //I am always here, trying to print string (typeJSON->valuestring) results in kernel panic
}
我会很感激任何建议。