AnsweredAssumed Answered

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

Question asked by 仕聪 蒋 on Feb 22, 2019
Latest reply on Jul 22, 2019 by larry wei

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个数组,运行打印如下:

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

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

谢谢!!!

Outcomes