最近弄了下jsonc解析数据,记录一下,方便以后查阅。
json_object* json_tokener_parse(const char *str);
json_object* json_object_from_file(const char *filename);
json_object* json_object_object_get(json_object* jso, const char *key)
//转int类型
int32_t json_object_get_int(struct json_object *obj);
//转string类型
const char* json_object_get_string(struct json_object *obj);
json_object* json_object_array_get_idx(struct json_object *obj , int idx);
json_object_put(struct json_object *obj);
二、实例
数据
{
"program":[
{
"assetId":"1595839049646290",
"channelId":"102457500",
"channelName":"TEST1",
"dayOfWeek":"6",
"endDateTime":"20200802012100",
"programId":"102456699",
"programName":"test1",
"startDateTime":"20200801233500",
"status":"1",
"viewLevel":"0"
},
{
"assetId":"1595839049643424",
"channelId":"102457500",
"channelName":"TEST1",
"dayOfWeek":"6",
"endDateTime":"20200801233500",
"programId":"102456698",
"programName":"test1",
"startDateTime":"20200801215900",
"status":"1",
"viewLevel":"0"
}
],
"restartAtToken":"1000",
"totalResults":"29"
}
解析
#include
#include
#include
#include "json_object.h"
#include "json_object_private.h"
#include "jsonDataParse.h"
#include "debugUtils.h"
#include "bits.h"
//#define DEBUG 1
int iParseJsonString(json_object *retJson, char *pcKey, char *pcValue, int length)
{
json_object *jsTemp = NULL;
if(retJson == NULL || is_error(jsTemp))
{
printf("the retJson is error \n");
return -1;
}
jsTemp = json_object_object_get(retJson, pcKey);
if(NULL == jsTemp || is_error(jsTemp))
{
printf("the jsTemp : NULL\n");
return -1;
}
strncpy(pcValue, (char *)json_object_get_string(jsTemp), length);
pcValue[length -1] = '\0';
return 0;
}
int iParseJsonint(json_object *retJson, char *pcKey, int *pcValue)
{
json_object *jsTemp = NULL;
if(retJson == NULL || is_error(jsTemp))
{
printf("the retJson is error \n");
return -1;
}
jsTemp = json_object_object_get(retJson, pcKey);
if(NULL == jsTemp || is_error(jsTemp))
{
printf("the jsTemp : NULL\n");
return -1;
}
*pcValue = (int)json_object_get_int(jsTemp);
return 0;
}
SHOW_LIST_t * parse_showlist(char *databuf, int *length)
{
int i = 0;
SHOW_LIST_t *listdata = NULL;
char totalResults[8] = {0};
char restartAtToken[8] = {0};
json_object *jsonobj = NULL;
json_object *tmpjson = NULL;
json_object *datajson = NULL;
if(NULL == databuf)
{
printf("the databuf is NULL ...\n");
return NULL;
}
jsonobj = json_tokener_parse(databuf);
if (is_error(jsonobj))
{
printf("the jsonobj is error \n");
return NULL;
}
iParseJsonString(jsonobj, "totalResults", totalResults, sizeof(totalResults));
if(0 == strlen(totalResults)){
printf("totalResults is NULL\n");
return NULL;
}
(*length) = atoi(totalResults);
if((*length) <= 0){
printf("totalResults : 0\n");
return NULL;
}
GUI_DEBUG("totalResults : %s\n",totalResults);
iParseJsonString(jsonobj, "restartAtToken", restartAtToken, sizeof(restartAtToken));
GUI_DEBUG("restartAtToken : %s\n",restartAtToken);
listdata = (struct SHOW_LIST_t *)malloc(sizeof(struct SHOW_LIST_t)*(*length));
if (NULL == listdata){
printf("parse_showlist : malloc error \n");
return NULL;
}
memset(listdata, 0, sizeof(struct SHOW_LIST_t)*(*length));
tmpjson = json_object_object_get(jsonobj , "program");
for(i = 0; i <(*length); i++)
{
datajson = json_object_array_get_idx(tmpjson , i);
if(NULL == datajson){
break;
}
iParseJsonString(datajson, "assetId", listdata[i].assetId, sizeof(listdata[i].assetId));
iParseJsonString(datajson, "channelId", listdata[i].channelId, sizeof(listdata[i].channelId));
iParseJsonString(datajson, "channelName", listdata[i].channelName, sizeof(listdata[i].channelName));
iParseJsonString(datajson, "dayOfWeek", listdata[i].dayOfWeek, sizeof(listdata[i].dayOfWeek));
iParseJsonString(datajson, "endDateTime", listdata[i].endDateTime, sizeof(listdata[i].endDateTime));
iParseJsonString(datajson, "programId", listdata[i].programId, sizeof(listdata[i].programId));
iParseJsonString(datajson, "programName", listdata[i].programName, sizeof(listdata[i].programName));
iParseJsonString(datajson, "startDateTime", listdata[i].startDateTime, sizeof(listdata[i].startDateTime));
iParseJsonString(datajson, "status", listdata[i].status, sizeof(listdata[i].status));
iParseJsonString(datajson, "viewLevel", listdata[i].viewLevel, sizeof(listdata[i].viewLevel));
}
json_object_put(jsonobj);
#ifdef DEBUG
GUI_DEBUG("------------the program -----------------\n");
SHOW_LIST_t *oo = listdata;
GUI_DEBUG("{\n\t\"program\":\n\t\t[\n");
for(i = 0 ; i < (*length); i++)
{
GUI_DEBUG("\t\t\t\{\"id\":\"%s\",\"name\":\"%s\",\"type\":%s,\"count\":%s\}\n",oo->assetId , oo->channelId,oo->channelName,oo->dayOfWeek);
oo++;
}
GUI_DEBUG("\t\t],\n\t\"totalResults\":%s,\n\t\"restartAtToken\":%s,\n\t\n}\n",totalResults, restartAtToken);
#endif
return listdata;
}
三、结束语
解析json数据四步走:
1、将其转化成json对象
2、解析成json对象
3、转化成对应的数据类型
4、释放json对象
5、需要强调的一点是错误判断不是判空能检测出来的。要用到is_error接口,如下使用:
jsonobj = json_tokener_parse(databuf);
if (is_error(jsonobj))
{
printf("the jsonobj is error \n");
return NULL;
}