1.引言
? ? ? ? 特殊的无key值的json数组今天遇到了,所以研究了会才知道怎么生成和解析这种无键值的json数组。下面我们来看看今天的实验。
2.json生成数组实验
????????今天的实验就是生成这样的无key值的json数组,在这里称为数组1。
{
"scanPrefix": [
"hello",
"hello",
"hello",
"hello",
"hello"
]
}
????????我们再来看看正常一点的有key值的json数组,在这里称为数组2。
{
"scanPrefix":[
{"H":"hello"},
{"H":"hello"},
{"H":"hello"},
{"H":"hello"},
{"H":"hello"}
]
}
????????上述json数组区别在于有无键值,即有无key值json数组。
来看看数组1的生成方式:
uint16_t protocol_Respond_Array0(uint8_t *pRcvBufferOut, uint16_t nlen)
{
uint16_t ni = 0;
uint16_t nsize = 0;
char *data_str = NULL;
char *response_str = NULL;
cJSON *response_root = NULL;
//生成Json数据格式
response_root = cJSON_CreateObject();
cJSON *scanPrefixArray = cJSON_CreateArray();
cJSON_AddItemToObject(response_root, "scanPrefix", scanPrefixArray);
for(ni = 0; ni <5 ; ni++)
{
if(ni%2==0)
cJSON_AddStringToObject(scanPrefixArray, "NULL","hello");
else
cJSON_AddStringToObject(scanPrefixArray, "NULL","hi");
}
response_str = cJSON_Print(response_root);
nsize = strlen(response_str);
if(pRcvBufferOut != NULL)
{
memcpy(pRcvBufferOut, response_str, nsize);
}
if(NULL !=response_root) cJSON_Delete(response_root);
if(NULL !=response_str) free(response_str);
return nsize;
}
来看看数组2的生成方式:
uint16_t protocol_Respond_Array0(uint8_t *pRcvBufferOut, uint16_t nlen)
{
uint16_t ni = 0;
uint16_t nsize = 0;
char *data_str = NULL;
char *response_str = NULL;
cJSON *response_root = NULL;
//生成Json数据格式
response_root = cJSON_CreateObject();
cJSON *scanPrefixArray = cJSON_CreateArray();
cJSON_AddItemToObject(response_root, "scanPrefix", scanPrefixArray);
for(ni = 0; ni <5 ; ni++)
{
cJSON*obj=cJSON_CreateObject();
cJSON_AddItemToArray(scanPrefixArray,obj);
if(ni%2==0)
cJSON_AddStringToObject(obj, "H","hello");
else
cJSON_AddStringToObject(obj, "H","hi");
}
response_str = cJSON_Print(response_root);
nsize = strlen(response_str);
if(pRcvBufferOut != NULL)
{
memcpy(pRcvBufferOut, response_str, nsize);
}
if(NULL !=response_root) cJSON_Delete(response_root);
if(NULL !=response_str) free(response_str);
return nsize;
}
3.解析json数组实验?
? ? ? ? 解析无key值json数组1。
uint16_t protocol_parse_Array0(uint8_t *pRcvBufferOut, uint16_t nlen)
{
cJSON *item = NULL;
cJSON *pRcvJson = NULL;
cJSON *pRcvJsonSec = NULL;
pRcvJson = cJSON_Parse((char *)pRcvBufferOut);
if(pRcvJson == NULL)
{
printf("cJSON_Parse failed\r\n");
return 0;
}
/*分解json类型信息*/
pRcvJsonSec=cJSON_GetObjectItem(pRcvJson,"scanPrefix");
if(pRcvJsonSec ==NULL)
{
printf("cJSON_Parse method failed\r\n");
return 0;
}
int arraysize = cJSON_GetArraySize(pRcvJsonSec);
for(int i=0;i<arraysize;i++)
{
item=cJSON_GetArrayItem(pRcvJsonSec,i);
if(item != NULL)
{
printf("======>%s\r\n",item->valuestring);
}
}
if(NULL !=pRcvJson) cJSON_Delete(pRcvJson);
return 0;
}
?????解析有key值json数组2。
uint16_t protocol_parse_Array(uint8_t *pRcvBufferOut, uint16_t nlen)
{
cJSON *item = NULL;
cJSON *pRcvJson = NULL;
cJSON *pRcvJsonSec = NULL;
pRcvJson = cJSON_Parse((char *)pRcvBufferOut);
if(pRcvJson == NULL)
{
printf("cJSON_Parse failed\r\n");
return 0;
}
/*分解json类型信息*/
pRcvJsonSec=cJSON_GetObjectItem(pRcvJson,"scanPrefix");
if(pRcvJsonSec ==NULL)
{
printf("cJSON_Parse method failed\r\n");
return 0;
}
int arraysize = cJSON_GetArraySize(pRcvJsonSec);
for(int i=0;i<arraysize;i++)
{
item=cJSON_GetArrayItem(pRcvJsonSec,i);
if(item != NULL)
{
cJSON *itemread=cJSON_GetObjectItem(item,"H");
printf("H:%s\n",itemread->valuestring);
}
}
if(NULL !=pRcvJson) cJSON_Delete(pRcvJson);
return 0;
}
?今天实验到此结束!over!
|