用C讀取json檔案

2022-02-15 07:23:08 字數 1373 閱讀 4231

a

jconf_t *

read_jconf(const char *file)

// 確定檔案大小, 並將讀取指標移回檔案開始

fseek(f, 0, seek_end);

long pos = ftell(f);

fseek(f, 0, seek_set);

if (pos < 0)

if (pos >= max_conf_size)

// 根據檔案大小申請記憶體空間, 注意這邊要多申請1個位元組用於存放'\0'

buf = ss_malloc(pos + 1);

if (buf == null)

// 讀取檔案內容至buff

int nread = fread(buf, pos, 1, f);

if (!nread)

// 關閉檔案

fclose(f);

// 關閉字串結尾

buf[pos] = '\0'; // end of string

json_settings settings = ;

char error_buf[512];

// 解析內容至結構化json資料

obj = json_parse_ex(&settings, buf, pos, error_buf);

if (obj == null)

if (obj->type == json_object)

json_value *v = value->u.array.values[j];

char *addr_str = to_string(v);

parse_addr(addr_str, conf.remote_addr + j);

ss_free(addr_str);

conf.remote_num = j + 1;

}} else if (value->type == json_string)

} else if (strcmp(name, "port_password") == 0)

json_value *v = value->u.object.values[j].value;

if (v->type == json_string) }}

} else if (strcmp(name, "server_port") == 0)

// ...

}} else

// 釋放檔案內容記憶體空間

do while (0);

// 釋放結構化json資料空間

json_value_free(obj);

return &conf;

}void *

ss_malloc(size_t size)

用C讀取json檔案

a jconf t read jconf const char file 確定檔案大小,並將讀取指標移回檔案開始 fseek f,0,seek end long pos ftell f fseek f,0,seek set if pos 0 if pos max conf size 根據檔案大小申請...

C 讀取json檔案內容

int main int argc,const char argv return runtest opts catch const std exception e std string strvalue json reader reader json解析 json value value 表示乙個j...

C 讀取excel檔案,生成json

這段時間在做乙個資料管理系統,需要讀取excel 組裝json,最終存入mongodb,這裡記錄一下解決思路。需要做的準備工作 1.安裝accessdatabaseengine 有32bit和64bit兩個版本,具體要安裝哪個要看你的office程式是32bit的還是64bit的。通常來講,一般是選...