日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Json文件解析(上)

發(fā)布時(shí)間:2023/11/28 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Json文件解析(上) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Json文件解析(上)

代碼地址:https://github.com/nlohmann/json

自述文件
alt=GitHub贊助商
data-canonical-src=“https://img.shields.io/badge/GitHub-Sponsors-ff69b4”
v:shapes="_x0000_i1025">

設(shè)計(jì)目標(biāo)
贊助商
積分

CMake的
包裝經(jīng)理
包配置

例子

JSON作為一流的數(shù)據(jù)類型
序列化/反序列化
類似STL的訪問
從STL容器轉(zhuǎn)換
JSON指針和JSON補(bǔ)丁
JSON合并補(bǔ)丁
隱式轉(zhuǎn)換
到/從任意類型的轉(zhuǎn)換
專門進(jìn)行枚舉轉(zhuǎn)換
二進(jìn)制格式(BSON,CBOR,MessagePack和UBJSON)

支持的編譯器
執(zhí)照
聯(lián)系
謝謝
二手第三方工具
使用JSON for Modern C ++的項(xiàng)目
筆記
執(zhí)行單元測(cè)試

設(shè)計(jì)目標(biāo)

那里有無數(shù)的JSON庫(kù),每個(gè)庫(kù)甚至都有其存在的理由。班有以下設(shè)計(jì)目標(biāo):

直觀的語(yǔ)法。在Python等語(yǔ)言中,JSON感覺就像是一流的數(shù)據(jù)類型。使用了現(xiàn)代C ++的所有操作符魔術(shù),以在代碼中實(shí)現(xiàn)相同的感覺。查看以下示例,將了解意思。
微不足道的整合。整個(gè)代碼包含一個(gè)頭文件json.hpp。沒有庫(kù),沒有子項(xiàng)目,沒有依賴項(xiàng),沒有復(fù)雜的構(gòu)建系統(tǒng)。該類用香草C ++ 11編寫。總而言之,一切都不需要調(diào)整編譯器標(biāo)志或項(xiàng)目設(shè)置。
認(rèn)真測(cè)試。項(xiàng)目經(jīng)過嚴(yán)格的單元測(cè)試,涵蓋了100%的代碼,包括所有異常行為。此外,使用Valgrind和Clang網(wǎng)絡(luò)檢查是否有內(nèi)存泄漏。Google OSS-Fuzz還針對(duì)所有解析器24/7運(yùn)行模糊測(cè)試,到目前為止,有效執(zhí)行了數(shù)十億次測(cè)試。為了保持高質(zhì)量,該項(xiàng)目遵循核心基礎(chǔ)設(shè)施計(jì)劃(CII)最佳實(shí)踐。

其方面對(duì)而言并不那么重要:

存儲(chǔ)效率。每個(gè)JSON對(duì)象的開銷為一個(gè)指針(聯(lián)合的最大大小)和一個(gè)枚舉元素(1個(gè)字節(jié))。默認(rèn)歸納使用以下C ++數(shù)據(jù)類型:std::string用于字符串int64_t,uint64_t或double數(shù)字,std::map對(duì)象,std::vector數(shù)組和bool布爾值。但是,可以根據(jù)basic_json需要對(duì)通用類進(jìn)行模板化。
速度。當(dāng)然,那里有更快的JSON庫(kù)。但是,如果目標(biāo)是通過使用單個(gè)標(biāo)頭添加JSON支持來加快開發(fā)速度,那么該庫(kù)就是理想之選。如果知道如何使用std::vector或std::map,那么已經(jīng)設(shè)置好了。

積分

json.hpp是此處single_include/nlohmann或發(fā)布的單個(gè)必需文件。需要添加

#包括 < nlohmann /
json.hpp >

//為了方便

使用 json = nlohmann
:: json;

到要處理JSON的文件,并設(shè)置必要的開關(guān)以啟用C ++ 11(例如,-std=c++11對(duì)于GCC和Clang)。

可以進(jìn)一步使用file include/nlohmann/json_fwd.hpp進(jìn)行前向聲明。json_fwd.hpp的安裝(作為cmake安裝步驟的一部分)可以通過設(shè)置來實(shí)現(xiàn)-DJSON_MultipleHeaders=ON。

CMake的

也可以nlohmann_json::nlohmann_json在CMake中使用接口目標(biāo)。此目標(biāo)填充適當(dāng)?shù)氖褂靡?#xff0c;INTERFACE_INCLUDE_DIRECTORIES以指向適當(dāng)?shù)陌夸浐虸NTERFACE_COMPILE_FEATURES必要的C ++ 11標(biāo)志。

外部

要從CMake項(xiàng)目中使用此庫(kù),可以直接從中找到,find_package()并使用生成的程序包配置中的命名空間導(dǎo)入目標(biāo):

#CMakeLists.txt

find_package(nlohmann_json
3.2.0 REQUIRED)

add_library(foo …)

target_link_libraries(foo PRIVATE nlohmann_json
:: nlohmann_json)

軟件包配置文件nlohmann_jsonConfig.cmake可以在安裝樹中使用,也可以在構(gòu)建樹之外直接使用。

嵌入式的

要將庫(kù)直接嵌入到現(xiàn)有CMake項(xiàng)目中,請(qǐng)將整個(gè)源代碼樹放置在子目錄中,然后調(diào)用add_subdirectory()CMakeLists.txt文件:

#通常不那么關(guān)心第三方庫(kù)的測(cè)試是

#從自己的項(xiàng)目的代碼運(yùn)行。

設(shè)置(JSON_BuildTests OFF CACHE INTERNAL “”)

#如果僅在私有源文件中包含此第三方,則

#在安裝主項(xiàng)目時(shí)不需要安裝。

#集(JSON_Install OFF
CACHE INTERNAL “”)

#請(qǐng)勿使用include(nlohmann_json / CMakeLists.txt),因?yàn)闀?huì)附帶

#會(huì)導(dǎo)致構(gòu)建中斷的意外后果。通常

#鼓勵(lì)(雖然不一定有據(jù)可查這樣)使用

#包括(…),用于其項(xiàng)目的CMake反正拉動(dòng)。

add_sub目錄(nlohmann_json)

add_library(foo …)

target_link_libraries(foo PRIVATE nlohmann_json
:: nlohmann_json)

嵌入式(FetchContent)

從CMake v3.11開始, FetchContent可以作為配置類型的依賴項(xiàng)自動(dòng)下載資源庫(kù)。

例:

包括(FetchContent)

FetchContent_Declare(json

GIT_REPOSITORY https://github.com/nlohmann/json.git

GIT_TAG
v3.7.3)

FetchContent_GetProperties(json)

如果(NOT json_POPULATED)

FetchContent_Populate(json)

add_subdirectory($ {json_SOURCE_DIR} $ {json_BINARY_DIR} EXCLUDE_FROM_ALL)

endif()

target_link_libraries(foo PRIVATE nlohmann_json
:: nlohmann_json)

注意:倉(cāng)庫(kù)https://github.com/nlohmann/json的下載量很大。包含用于基準(zhǔn)測(cè)試的所有數(shù)據(jù)集。可能需要依賴較小的存儲(chǔ)庫(kù)。例如,可能想用https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent替換上面的URL

同時(shí)支持

為了允許項(xiàng)目支持外部提供的或嵌入式JSON庫(kù),可以使用類似于以下內(nèi)容的模式:

# Top level CMakeLists.txtproject(FOO)…option(FOO_USE_EXTERNAL_JSON “Use an external JSON library” OFF)…add_subdirectory(thirdparty)…add_library(foo …)…# Note that the namespaced target will always be available regardless of the# import methodtarget_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)# thirdparty/CMakeLists.txt…if(FOO_USE_EXTERNAL_JSON) find_package(nlohmann_json 3.2.0 REQUIRED)else() set(JSON_BuildTests OFF CACHE INTERNAL “”) add_subdirectory(nlohmann_json)endif()…

thirdparty/nlohmann_json 然后是此源樹的完整副本。

包管理Package Managers

If
you are using OS X and Homebrew, just
type brew tap nlohmann/json and brew install nlohmann-json and
you’re set. If you want the bleeding edge rather than the latest release, use brew install nlohmann-json --HEAD.

If
you are using the Meson Build System,
add this source tree as a meson subproject. You may also use the include.zip published in this
project’s Releases to reduce the size of the vendored source
tree. Alternatively, you can get a wrap file by downloading it from Meson WrapDB,
or simply use meson wrap install nlohmann_json. Please see the meson project for any issues regarding the
packaging.

The
provided meson.build can also be used as an alternative to cmake for installing nlohmann_json system-wide in
which case a pkg-config file is installed. To use it, simply have your build
system require the nlohmann_json pkg-config dependency. In Meson, it is preferred to use
the dependency() object
with a subproject fallback, rather than using the subproject directly.

If
you are using Conan to
manage your dependencies, merely add nlohmann_json/x.y.z to your conanfile’s requires, where x.y.z is the release version you want to use. Please file issues here if
you experience problems with the packages.

If
you are using Spack to
manage your dependencies, you can use the nlohmann-json package. Please see the spack project for any
issues regarding the packaging.

If you are using hunter on your project for external dependencies, then
you can use the nlohmann_json package. Please see the hunter project for
any issues regarding the packaging.

If
you are using Buckaroo, you
can install this library’s module with buckaroo add
github.com/buckaroo-pm/nlohmann-json.
Please file issues here. There is a demo repo here.

If you are using vcpkg on
your project for external dependencies, then you can use the nlohmann-json package. Please see the vcpkg project for any
issues regarding the packaging.

If
you are using cget, you can install the latest development version with cget install nlohmann/json. A
specific version can be installed with cget install
nlohmann/json@v3.1.0. Also, the multiple header
version can be installed by adding the -DJSON_MultipleHeaders=ON flag (i.e., cget install
nlohmann/json -DJSON_MultipleHeaders=ON).

If
you are using CocoaPods, you
can use the library by adding pod “nlohmann_json”,
‘~>3.1.2’ to your podfile (see an example). Please file issues here.

If you are using NuGet, you can use the package nlohmann.json.
Please check this extensive description on how to use the package.
Please files issues here.

If
you are using conda, you can
use the package nlohmann_json from conda-forge executing conda install -c conda-forge nlohmann_json. Please file issues here.

If
you are using MSYS2, your
can use the mingw-w64-nlohmann-json package, just type pacman -S mingw-w64-i686-nlohmann-json or pacman -S
mingw-w64-x86_64-nlohmann-json for
installation. Please file issues here if you experience problems with the packages.

If
you are using build2, you can use the nlohmann-json package from the
public repository https://cppget.org or
directly from the package’s sources repository. In your
project’s manifest file, just add depends:
nlohmann-json(probably with some version constraints). If you are not familiar with using
dependencies in build2, please read this introduction. Please file issues here if
you experience problems with the packages.

If
you are using wsjcpp, you can use the command wsjcpp install
“https://github.com/nlohmann/json:develop” to get the latest version. Note you can change the branch
“:develop” to an existing tag or another branch。

包配置

如果使用的是裸Makefile,則可以使用pkg-config生成指向庫(kù)安裝位置的include標(biāo)志:

pkg-config nlohmann_json --cflags

Meson構(gòu)建系統(tǒng)的用戶還可以使用系統(tǒng)范圍的庫(kù),該庫(kù)可以通過pkg-config以下方式找到:

json = dependency(‘nlohmann_json’, required: true)

例子

除了下面的示例,可能想查看文檔,其中每個(gè)函數(shù)都包含一個(gè)單獨(dú)的代碼示例(例如,檢出emplace())。所有示例文件都可以單獨(dú)編譯和執(zhí)行(例如,文件emplace.cpp)。

JSON作為一流的數(shù)據(jù)類型

以下是一些示例,可讓了解如何使用該類。

假設(shè)要?jiǎng)?chuàng)建JSON對(duì)象

{ “pi”: 3.141, “happy”: true, “name”: “Niels”, “nothing”: null, “answer”: { “everything”: 42 }, “l(fā)ist”: [1, 0, 2], “object”: { “currency”: “USD”, “value”: 42.99 }}

使用該庫(kù),可以編寫:

//創(chuàng)建一個(gè)空結(jié)構(gòu)(空)

json j;

//添加一個(gè)存儲(chǔ)為double的數(shù)字(注意j到對(duì)象的隱式轉(zhuǎn)換)

j [ “ pi ” ] = 3.141 ;

//添加一個(gè)存儲(chǔ)為bool

j [ “ happy ” ] = true的布爾值;

//添加一個(gè)存儲(chǔ)為std :: string

j [ “ name ” ] = “ Niels ”的字符串;

//通過傳遞nullptr

j [ “ nothing ” ] = nullptr來添加另一個(gè)null對(duì)象;

//在對(duì)象

j [ “ answer ” ] [ “ everything ” ] = 42 內(nèi)添加一個(gè)對(duì)象;

//添加存儲(chǔ)為標(biāo)準(zhǔn)::向量(使用初始化列表)的陣列

f] [ “列表” ] = { 1, 0, 2 };

//添加另一個(gè)對(duì)象(使用成對(duì)的初始化列表)

j [ “ object ” ] = {{ “ currency ”, “ USD ” },{ “ value ”, 42.99 }};

//相反,也可以編寫(看起來與上面的JSON非常相似)

json j2 = {

{ “ pi ”,3.141 },

{ “ happy ”,true },

{ “ name ”,“ Niels ” },

{ “沒有”,nullptr },

{ “答案”,{

{

“一切”,42 }

}},

{ “列表”,{ 1,0,2 }},

{ “對(duì)象”,{

{

“貨幣”,“美元” },

{

“值”,42.99 }

}}

};

請(qǐng)注意,在所有這些情況下,都無需“告訴”編譯器要使用的JSON值類型。如果想明確表達(dá)或表達(dá)一些極端情況,這些函數(shù)json::array()和json::object()將有助于:

//表達(dá)空數(shù)組[]的方法

json empty_array_explicit = json :: array();

//表示空對(duì)象的方法{}

json empty_object_implicit = json({});

json empty_object_explicit = json :: object();

//一種方式來表達(dá)的鍵/值對(duì)的_array_ [[ “貨幣”, “USD”],[ “值”,42.99]]

JSON array_not_object = JSON ::陣列({{ “貨幣”, “ USD ” } ,{ “ value ”, 42.99 }});

序列化/反序列化

到/從字符串

可以通過附加_json到字符串文字來創(chuàng)建JSON值(反序列化):

//從字符串文字

json創(chuàng)建對(duì)象 j = “ { \” happy \“:true,\” pi \“: 3.141 } ” _json;

//甚至使用原始字符串文字

自動(dòng) j2 = R“(

{

happy“:true,

pi“:

3.141
}

)” _json;

請(qǐng)注意,不附加_json后綴,傳遞的字符串文字不被解析,而僅用作JSON字符串值。也就是說,json j = "{ “happy”:
true, “pi”: 3.141 }“將只存儲(chǔ)字符串”{
“happy”: true, “pi”: 3.141 }"而不是解析實(shí)際對(duì)象。

上面的示例也可以使用來明確表示json::parse():

//明確解析

自動(dòng) j3 = json ::
parse( “ { \” happy \“:true,\” pi \“:3.141} ”);

還可以獲取JSON值的字符串表示形式(序列化):

//明確轉(zhuǎn)換為字符串

std :: string s = j.dump(); // {“ happy”:true,“ pi”:3.141}

//通過漂亮的打印進(jìn)行序列化

//傳遞空格以縮進(jìn)

std :: cout << j.dump( 4)<< std ::
endl;

// {

//
“ happy”:true,

//
“ pi”:3.141

// }

注意序列化和賦值之間的區(qū)別:

//在JSON值中存儲(chǔ)一個(gè)字符串

json j_string = “這是一個(gè)字符串” ;

//檢索字符串值

auto cpp_string =
j_string.get ();

//檢索字符串值(變量已經(jīng)存在時(shí)的替代方法)

std :: string cpp_string2;

j_string.get_to(cpp_string2);

//檢索序列化的值(顯式JSON序列化)

std :: string serialized_string =
j_string.dump();

//輸出原始字符串

std :: cout << cpp_string << “ == ” <<
cpp_string2 << “ == ” <<
j_string.get ()<< ’ \ n ’ ;

//輸出序列化值

std :: cout << j_string << “ == ” <<
serialized_string << std :: endl;

.dump()始終返回序列化的值,并.getstd::string()返回原始存儲(chǔ)的字符串值。

請(qǐng)注意,該庫(kù)僅支持UTF-8。當(dāng)在庫(kù)中存儲(chǔ)具有不同編碼的字符串時(shí),dump()除非json::error_handler_t::replace或json::error_handler_t::ignore用作錯(cuò)誤處理程序,否則調(diào)用可能會(huì)引發(fā)異常。

往返流(例如文件,字符串流)

還可以使用流來序列化和反序列化:

//從標(biāo)準(zhǔn)輸入反序列化

json j;

std :: cin >> j;

//序列化為標(biāo)準(zhǔn)輸出

std :: cout << j;

// setw操縱器過載以設(shè)置漂亮打印的縮進(jìn)

std :: cout << std :: setw( 4)<< j
<< std :: endl;

這些運(yùn)算符可用于std::istream或的任何子類std::ostream。這是文件的相同示例:

//讀取JSON文件

std :: ifstream i( “ file.json ”);

json j;

j;

//將經(jīng)過修飾的JSON寫入另一個(gè)文件

std :: ofstream o( “ pretty.json ”);

o << std :: setw(4)<< j
<< std :: endl;

請(qǐng)注意,為此設(shè)置異常位failbit是不合適的。由于使用了noexcept指定符,將導(dǎo)致程序終止。

從迭代器范圍讀取

也可以從迭代器范圍解析JSON;也就是從迭代器可訪問的,value_type整數(shù)類型為1、2或4個(gè)字節(jié)的任何容器中,這些容器將分別解釋為UTF-8,UTF-16和UTF-32。例如std::vectorstd::uint8_t,或std::liststd::uint16_t:

std :: vector v = { ’ t ‘,’ r ‘,’ u ‘,’ e ’ };

json j = json :: parse(v.begin(),v.end());

可以將迭代器保留在[begin,end)范圍內(nèi):

std :: vector v = { ’ t ‘,’ r ‘,’ u ‘,’ e ’ };

json j = json :: parse(v);

自定義數(shù)據(jù)源

由于parse函數(shù)接受任意迭代器范圍,因此可以通過實(shí)現(xiàn)此LegacyInputIterator概念來提供自己的數(shù)據(jù)源。

struct MyContainer {

void advance();

const char&get_current();

};

struct MyIterator {

 使用 difference_type = std :: ptrdiff_t ;使用 value_type = char ;使用指針= const  char *;使用 reference = const  char&;使用 iterator_category = std ::

input_iterator_tag;

MyIterator&運(yùn)算符 ++(){

MyContainer。前進(jìn)();

返回 * 這個(gè) ;

}布爾 運(yùn)算符!=(const MyIterator&rhs)const {

return rhs。目標(biāo)!=目標(biāo);

}引用運(yùn)算符 *()const {

返回目標(biāo)。get_current();

}

MyContainer * target = nullptr ;

};

MyIterator 開始(MyContainer&tgt){

 return MyIterator {&tgt};

}

MyIterator 結(jié)束(const MyContainer&){

 return {};

}

無效 foo(){

MyContainer c;

json j = json :: parse(c);

}

SAX接口

該庫(kù)使用具有以下功能的類SAX接口:

//當(dāng)解析為

null 時(shí)調(diào)用bool null();

//在解析布爾值時(shí)調(diào)用;值傳遞給

布爾布爾 值(布爾值);

//在解析有符號(hào)或無符號(hào)整數(shù)時(shí)調(diào)用;值傳遞給

bool number_integer( number_integer_t val);

bool number_unsigned( number_unsigned_t val);

//在解析浮點(diǎn)數(shù)時(shí)調(diào)用;值和原始字符串傳遞給

bool number_float( number_float_t val, const string_t&s);

//解析字符串時(shí)調(diào)用;值被傳遞并且可以安全地移開

布爾 字符串( string_t&val);

//分別在對(duì)象或數(shù)組開始或結(jié)束時(shí)調(diào)用。傳遞的元素?cái)?shù)量(如果不知道,則傳遞-1)

bool start_object(std :: size_t元素);

bool end_object();

bool start_array(std :: size_t元素);

bool end_array();

//在解析對(duì)象鍵時(shí)調(diào)用;值被傳遞并且可以安全地移開

bool 鍵( string_t&val);

//發(fā)生解析錯(cuò)誤時(shí)調(diào)用;字節(jié)位置,最后一個(gè)標(biāo)記和一個(gè)異常被傳遞給

bool parse_error(std :: size_t position, const std :: string&last_token, const detail :: exception&ex);

每個(gè)函數(shù)的返回值確定是否應(yīng)該進(jìn)行解析。

要實(shí)現(xiàn)自己的SAX處理程序,請(qǐng)按照下列步驟操作:

在一個(gè)類中實(shí)現(xiàn)SAX接口。可以將類nlohmann::json_sax用作基類,但也可以使用實(shí)現(xiàn)了上述功能并且將其公開的任何類。
創(chuàng)建SAX接口類的對(duì)象,例如my_sax。
致電bool
json::sax_parse(input, &my_sax); 其中第一個(gè)參數(shù)可以是任何輸入(例如字符串或輸入流),第二個(gè)參數(shù)是指向SAX接口的指針。

注意,該sax_parse函數(shù)僅返回一個(gè)bool指示上一次執(zhí)行的SAX事件的結(jié)果的結(jié)果。不返回 json值-由決定如何處理SAX事件。此外,在解析錯(cuò)誤的情況下不會(huì)拋出異常-由決定如何將異常對(duì)象傳遞給parse_error實(shí)現(xiàn)。在內(nèi)部,SAX接口用于DOM解析器(類json_sax_dom_parser)以及接受器(json_sax_acceptor),請(qǐng)參見file json_sax.hpp。

類似STL的訪問

設(shè)計(jì)了JSON類,使其表現(xiàn)得像STL容器一樣。實(shí)際上,滿足了ReversibleContainer的要求。

//使用push_back創(chuàng)建一個(gè)數(shù)組

json j;

j.push_back(“ foo ”);

j.push_back(1);

j.push_back(true);

//還使用emplace_back

j.emplace_back( 1.78);

//

為(json :: iterator
it = j.begin(); it!= j.end(); ++ it)迭代數(shù)組 {

std
:: cout << * it << ’ \ n ’ ;

}

//基于范圍的

for( auto&element:j){

std
:: cout <<元素<< ’ \ n ’ ;

}

// getter / setter

const auto tmp = j [ 0 ] .get ();

j [ 1 ] = 42 ;

bool foo = j.at(2);

//比較

j == “ [ \” foo \“,42,true] ” _json; //正確

//其東西

j.size(); // 3個(gè)條目

j.empty(); // false

j.type(); // json :: value_t :: array

j.clear(); //數(shù)組再次為空

//便利類型檢查器

j.is_null();

j.is_boolean();

j.is_number();

j.is_object();

j.is_array();

j.is_string();

//創(chuàng)建一個(gè)對(duì)象

json o;

o [ “ foo ” ] = 23 ;

o [ “ bar ” ] = false ;

o [ “ baz ” ] = 3.141 ;

//還可以使用

emplace o.emplace( “ weather ”, “ sunny ”);

// //對(duì)象專用的迭代器成員函數(shù),

用于(json :: iterator
it = o.begin(); it!= o.end(); ++ it){

std
:: cout <<。key()<< “:” <<。value()<< “ \ n ” ;

}

//與

for( auto&el:o.items()){

std
:: cout << el。key()<< “:” << << value()<< “ \ n ” ;

}

//即使結(jié)構(gòu)化綁定(C ++ 17)更容易

為(自動(dòng)&[鍵,值]:o.items()){

std
:: cout <<鍵<< “:” <<值<< “ \ n ” ;

}

//找到一個(gè)條目

,如果(o.contains( “富”)){

//存在與鍵“foo”的條目

}

//或者通過find和一個(gè)迭代器

if(o.find( “ foo ”)!= o.end()){

//有一個(gè)鍵為“ foo”的條目

}

//或更簡(jiǎn)單地使用count()

int foo_present =
o.count( “ foo ”); // 1

int fob_present =
o.count( “ fob ”); // 0

//刪除條目

o.erase( “ foo ”);

總結(jié)

以上是生活随笔為你收集整理的Json文件解析(上)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。