MIMXRT1052-EVKB开发板使用cJSON库问题

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MIMXRT1052-EVKB开发板使用cJSON库问题

1,891 Views
jiangshicong
Contributor III

IDE:keil5

开发板:NXP  MIMXRT1052-EVKB

在玩rt1052开发板过程中,用到了cJSON库,我看keil5中支持cJSON库,于是我就直接添加库了,但是在使用过程中发现构建json格式的时候,在一个json对象里面只能添加两个数组。添加地三个的时候,root对象直接就为NULL了。一下是我构建json格式的函数:

char *TestBox_ExportButton_Function(void)
{
cJSON *root, *dir, *subdir;
char *out;
PRINTF("click Test End Save testreport \r\n");
root = cJSON_CreateObject();

dir = cJSON_CreateArray();
cJSON_AddItemToObject(root,"iTT",dir);
for(int i=0; i<2; i++)
{
subdir = cJSON_CreateObject();
cJSON_AddItemToObject(dir,"dira",subdir);
cJSON_AddStringToObject(subdir,"item","MCU");
cJSON_AddStringToObject(subdir,"state","failed");
cJSON_AddStringToObject(subdir,"comment","success");
}

out = cJSON_Print(root);
printf("line=%d, %s, out_length=%d\r\n", __LINE__, out, strlen(out));
cJSON_Delete(root);
return out;
}

//解析json格式

void CJson_parse(char *out)
{
cJSON *root, *Root, *Child, *arrayItem, *item, *name, *state, *comment;
char *pr = NULL,*na = NULL,*pa = NULL,*fl = NULL;
root = cJSON_Parse(out);
if(root)
{
Root = cJSON_GetObjectItem(root,"iTT");
if(Root)
{
int size = cJSON_GetArraySize(Root);
PRINTF("arraysize=%d\r\n",size);
PRINTF("Root:\r\n");
for(int i=0; i< size; i++)
{
arrayItem = cJSON_GetArrayItem(Root, i);

if(arrayItem)
{
PRINTF("start......\r\n");
item = cJSON_GetObjectItem(arrayItem,"item");
state = cJSON_GetObjectItem(arrayItem,"state");
comment = cJSON_GetObjectItem(arrayItem,"comment");
na = item->valuestring;
pa = state->valuestring;
fl = comment->valuestring;

PRINTF("item:%s\r\n",na);
PRINTF("state:%s\r\n",pa);
PRINTF("comment:%s\r\n",fl);
}
else
PRINTF("arrayItem is null\r\n");
}
}
else
PRINTF("Root is null\r\n");
}
else
PRINTF("root is null\r\n");
}

下面是给对象添加2个数组,运行打印如下:

微信图片_20190222193355.png

下面是添加3个数组,运行打印如下:

pastedImage_2.png

这是为什么呀???有大神遇到过这种情况吗??

谢谢!!!

Tags (1)
0 Kudos
4 Replies

1,636 Views
jiangshicong
Contributor III

非常感谢楼上的回答,已经解决了。其实这个问题是堆空间分配不够,导致调用cjson库的api时,malloc函数失败,直接返回null。后面改变堆空间大小后,就可以正常运行了。

0 Kudos

1,636 Views
larry_hung
Contributor I

嗨,請問一下,我也遇到相同的問題,可以分享你的code嗎?
我找不到修改堆疊空間的code在哪裡,謝謝

0 Kudos

1,636 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

感谢经验分享。

Mike

0 Kudos

1,636 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

Could you check this issue at [cJSON] community?

Thanks for the understanding.

best regards,

Mike

0 Kudos