Linux 應用 jsonc解析資料。

2021-10-08 22:07:55 字數 4329 閱讀 5095

最近弄了下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);
二、例項

資料

,

],"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)

jstemp = json_object_object_get(retjson, pckey);

if(null == jstemp || is_error(jstemp))

strncpy(pcvalue, (char *)json_object_get_string(jstemp), length);

pcvalue[length -1] = '\0';

return 0;

}int iparsejsonint(json_object *retjson, char *pckey, int *pcvalue)

jstemp = json_object_object_get(retjson, pckey);

if(null == jstemp || is_error(jstemp))

*pcvalue = (int)json_object_get_int(jstemp);

return 0;}

show_list_t * parse_showlist(char *databuf, int *length)

; char restartattoken[8] = ;

json_object *jsonobj = null;

json_object *tmpjson = null;

json_object *datajson = null;

if(null == databuf)

jsonobj = json_tokener_parse(databuf);

if (is_error(jsonobj))

iparsejsonstring(jsonobj, "totalresults", totalresults, sizeof(totalresults));

if(0 == strlen(totalresults))

(*length) = atoi(totalresults);

if((*length) <= 0)

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)

memset(listdata, 0, sizeof(struct show_list_t)*(*length));

tmpjson = json_object_object_get(jsonobj , "program");

for(i = 0; i <(*length); i++)

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",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))

Linux之ELF魔數解析

在linux系統下,我們通常可以使用readelf命令來檢視elf檔案資訊。例如 elf檔案有32位版本以及64版本之分,32位版本的結構體資訊如下 上面這些結構跟剛才輸出的kill0這個檔案的elf資訊基本一一對應,不同的是,elf32 ehdr中的e ident對應了readelf輸出中的 cl...

Spring Scheduled應用解析

最近,遇到的乙個需求,需要執行定時任務,每個一定時間需要執行某個方法 因為專案是springmvc的專案,所以使用的是spring scheduled 由於quartz應用起來太麻煩,所以沒有採用 接下來是應用步驟 1 配置檔案 1.1 需要配置一項 xmlns 如下 xmlns task spri...

python pyquery 解析html資料

python pyquery 解析html資料 1 windows環境cmd安裝擴充套件 pip install pyquery 3 pyquery 官方文件 api.html 4 from pyquery import pyquery as pq headers d pq url encoding...