关于CJSON添加数字

harryhu
Posts: 17
Joined: Thu Jan 21, 2016 9:28 pm

关于CJSON添加数字

Postby harryhu » Sun Apr 10, 2016 10:45 am

HI 乐鑫 :

在使用CJSON创建字符没有问题,但是创建数字是一直是显示0,代码如下:

/*
* Create a json format data
*/
root = cJSON_CreateObject();
if (root == NULL) {
os_printf("cJson create obj error!!\n");
vTaskDelete(NULL);
return;
}
cJSON_AddStringToObject(root,"msg_type","notify");

services = cJSON_CreateObject();
if (services == NULL) {
os_printf("cjson create services error!!\n");
vTaskDelete(NULL);
cJSON_Delete(root);
return;
}

cJSON_AddItemToObject(root,"services",services);

ops = cJSON_CreateObject();
if (ops == NULL) {
os_printf("cjson create ops error!!\n");
vTaskDelete(NULL);
cJSON_Delete(root);
cJSON_Delete(services);

return;
}

cJSON_AddItemToObject(services,"operation_status",ops);
cJSON_AddNumberToObject(ops,"status",1L);

li = cJSON_CreateObject();
if (li == NULL) {
os_printf("cjson create ops error!!\n");
vTaskDelete(NULL);
cJSON_Delete(root);
cJSON_Delete(services);

cJSON_Delete(li);

return;
}
cJSON_AddItemToObject(services,"lightbulb",li);
cJSON_AddNumberToObject(li,"alpha",122L);
cJSON_AddNumberToObject(li,"color_rgb",122L);
cJSON_AddNumberToObject(li,"color_hsl",122L);


jsonStr = cJSON_Print(root);

os_printf("json data = %s\n",jsonStr);


打印结果如下:
json data = {
"msg_type": "notify",
"services": {
"operation_status": {
"status": 0
},
"lightbulb": {
"alpha": 0,
"color_rgb": 0,
"color_hsl": 0
}
}
}


数字全部是0,请问这是怎么回事?用的SDK是ESP8266_RTOS_SDK-master.zip,是2016/2/20下载的

THS

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: 关于CJSON添加数字

Postby ESP_Faye » Thu Apr 14, 2016 5:52 pm

您好,

ESP8266_IOT_PLATFORM 是基于 ESP8266_RTOS_SDK 的应用示例,您可以参考它使用 cJSON_AddNumberToObject。

感谢您对 ESP8266 的关注!

harryhu
Posts: 17
Joined: Thu Jan 21, 2016 9:28 pm

Re: 关于CJSON添加数字

Postby harryhu » Fri Apr 22, 2016 4:46 pm

HI 乐鑫 :

我的用法和IOT中是一样的,数字还是为0

THS

Who is online

Users browsing this forum: No registered users and 1 guest