我打算对云平台上获取到的数据进行解析,但是出错了。当云平台下发数据,这里输出是这样的

但是经过复制到json解析网站是可以解析的

具体代码如下

unsigned char *getjson(unsigned char *str, int size)
{
for (int i = 0; i < size; i++)
{
if (str[i] == '{')
{
return str + i;
}
}
return NULL;
}
void USART2_IRQHandler(void)
{
//判断是否接收到数据
if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
USART2_RxFlag = 1;
//从串口外设接收数据 1次只能接收1个字节数据
UART2_RxBuf[UART2_RxCnt++] = USART_ReceiveData(USART2);
}
}
void json_pares(char *str)
{
cJSON *cjson_entire = NULL;
cJSON *cjson_waterlevel = NULL;
cJSON *cjson_feed = NULL;
cJSON *cjson_waterpump = NULL;
cJSON *cjson_weight = NULL;
cJSON *cjson_params = NULL;
cJSON *cjson_method = NULL;
printf("JSON数据:%s\r\n", str);
cjson_entire = cJSON_Parse(str);
if (cjson_entire == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
printf("Error before: %s\n", error_ptr);
}
printf("cjson_entire NULL\r\n");
}
else
{
printf("cjson_entire parsed successfully\r\n");
}
cjson_params = cJSON_GetObjectItem(cjson_entire, "params");
cjson_method = cJSON_GetObjectItem(cjson_entire, "method");
// char * data = cJSON_GetStringValue(cjson_params);
// printf("cjson_params=%s\r\n",data);
printf("====================JSON====================\n");
if (cjson_method != NULL)
printf("%s:%s\n", cjson_method->string, cjson_method->valuestring);
if (cjson_params != NULL)
{
cjson_feed = cJSON_GetObjectItem(cjson_params, "Feed");
cjson_waterlevel = cJSON_GetObjectItem(cjson_params, "WaterLevel");
cjson_waterpump = cJSON_GetObjectItem(cjson_params, "Water");
cjson_weight = cJSON_GetObjectItem(cjson_params, "Weight");
if (cjson_feed != NULL)
{
printf("%s:%d\n", cjson_feed->string, cjson_feed->valueint);
if(cjson_feed->valueint)
{
is_feed_on = true;
Forward_rotation(); // 开启喂食
delay_ms(50);
}
else
{
is_feed_on = false;
reversal(); //关闭喂食
delay_ms(50);
}
}
if (cjson_weight != NULL)
{
printf("%s:%d\n", cjson_weight->string, cjson_weight->valueint);
}
if (cjson_waterlevel != NULL)
{
printf("%s:%d\n", cjson_waterlevel->string, cjson_waterlevel->valueint);
}
if (cjson_waterpump != NULL)
{
printf("%s:%d\n", cjson_waterpump->string, cjson_waterpump->valueint);
if(cjson_waterpump->valueint)
{
is_pump_on = true;
GPIO_SetBits(GPIOG, GPIO_Pin_15); // 开启水泵
delay_ms(50);
}
else
{
is_pump_on = false;
GPIO_ResetBits(GPIOG, GPIO_Pin_15); // 关闭水泵
delay_ms(50);
}
}
}
printf("====================================");
cJSON_Delete(cjson_entire);
printf("\r\n");
}