C 讀寫Json檔案

2021-10-02 14:42:14 字數 3566 閱讀 6239

本文列舉了三種情況的**,並且還會繼續更新

1. 已有json檔案,並且了解其中內容,想指定更改某些內容

2. 已有json檔案,並且了解其中內容,想讀取它的內容

3. 沒有json檔案,想按照自己的想法寫乙個json檔案

例子:我的json檔案為example.json

,

"c":[110,120,119,911],

"d":"hello world!"

}

#include #include #include "rapidjson/document.h"

#include "rapidjson/writer.h"

#include "rapidjson/stringbuffer.h"

#include "rapidjson/filewritestream.h"

#include "rapidjson/prettywriter.h"

#include "rapidjson/filereadstream.h"

int main(int argc, char* ar**)

// 建立rapidjson的doc檔案

rapidjson::document doc;

// 讀取json檔案的內容

char readbuffer[65536];

rapidjson::filereadstream is(fp_read, readbuffer, sizeof(readbuffer));

doc.parsestream(is);

// 關閉json檔案

fclose(fp_read);

//讀取doc中你想更改的key,並更改

rapidjson::value& valuea = doc["a"];

valuea.setint(999);

int a = doc["a"].getint();

rapidjson::value& valueb1 = doc["b"]["b1"];

valueb1.setdouble(0.68001);

rapidjson::value& valueb2 = doc["b"]["b2"];

valueb2.setdouble(2.22);

rapidjson::value& valuec0 = doc["c"][0];

valuec0.setint(11);

rapidjson::value& valuec1 = doc["c"][1];

valuec1.setint(21);

rapidjson::value& valuec2 = doc["c"][2];

valuec2.setint(911);

rapidjson::value& valuec3 = doc["c"][3];

valuec3.setint(119);

rapidjson::value& valued = doc["d"];

valued.setstring("world hello!");

// 再以寫的方式開啟json檔案

file* fp_write = fopen(json_path.c_str(), "w");

// 建立rapidjson的writer

char writebuffer[65536];

rapidjson::filewritestream os(fp_write, writebuffer, sizeof(writebuffer));

rapidjson::prettywriterwriter(os);

// 將doc的更改寫進json檔案

doc.accept(writer);

// 關閉json檔案

fclose(fp_write);

return 0;

}

會發現json檔案變為了

,

"c": [

11,21,

911,

119],

"d": "world hello!"

}

讀取json檔案的**

#include #include #include "rapidjson/document.h"

#include "rapidjson/writer.h"

#include "rapidjson/stringbuffer.h"

#include "rapidjson/filewritestream.h"

#include "rapidjson/prettywriter.h"

#include "rapidjson/filereadstream.h"

int main(int argc, char* ar**)

char readbuffer[65536];

rapidjson::filereadstream is(fp, readbuffer, sizeof(readbuffer));

rapidjson::document doc;

doc.parsestream(is);

fclose(fp);

int a = doc["a"].getint();

double b1 = doc["b"]["b1"].getdouble();

double b2 = doc["b"]["b2"].getdouble();

double c[4];

c[0] = doc["c"][0].getdouble();

c[1] = doc["c"][1].getdouble();

c[2] = doc["c"][2].getdouble();

c[3] = doc["c"][3].getdouble();

std::string d = doc["d"].getstring();

std::cout《輸出結果為

read parameters

010086

11.1

110120

119911

hello world!

向指定json檔案寫入資料的**

#include #include #include "rapidjson/document.h"

#include "rapidjson/writer.h"

#include "rapidjson/stringbuffer.h"

#include "rapidjson/filewritestream.h"

#include "rapidjson/prettywriter.h"

#include "rapidjson/filereadstream.h"

int main(int argc, char* ar**)

寫入結果

,

"c": [

110,

120,

119,

911],

"d": "hello world!"

}

json讀寫檔案

jsonc 寫配置檔案比較簡單,並且解析配置檔案也比較省事。寫配置檔案 cpp view plain copy include include include include include inc json.h define config file config.json typedef stru...

php讀寫json檔案

生成乙個php陣列 data array 0 array a orange b banana 1 array 1,2,3,4,5,6 2 array first 5 second third data 3 id 30 data 3 content phperwei31 訪問二維陣列的方法 echo ...

python 讀寫json檔案

以字串的形式進行json讀寫 函式功能 json str json.dumps 要寫入的字典表 將字典表寫入json字串 json data json.loads json str 將json字串讀為字典表 import json json的true,false,null寫法和字典表 true,fa...