# include & ltiostream & gt
#包含" json/json.h "
使用命名空間std
int main()
{
//字符串
const char * str =
" {\"praenomen\":\"Gaius\ ",\"nomen\":\"Julius\ ",\"cognomen\":\"Caezar\ ","
" \ "生\":-100,\ "死\ ":-44 } ";
JSON::Reader Reader;
JSON::Value root;
//從字符串中讀取數據
if (reader.parse(str,root))
{
string praenomen = root[" praenomen "]。asString();
字符串nomen = root[ "nomen" ]。asString();
string cognomen = root[" cognomen "]。asString();
int born = root[ "born" ]。asInt();
int died = root[ "died" ]。asInt();
cout & lt& ltpraenome+" "+nomen+" "+cognomen
& lt& lt“出生年份”& lt& lt出生的
& lt& lt“,死於哪年”& lt& lt死了& lt& ltendl
}
返回0;
}
生成文件文件
LIB =-L/usr/local/LIB/LIB JSON/-ljson _ Linux-gcc-4 . 4 . 7 _ libmt
答:a.o
g++ -o a -std=c++0x a.o $(LIB)
a . o . a . CPP
g++ -c a.cpp
清潔:
RM-RF a o a
其次,從文件中讀取JSON
PersonalInfo.json(存儲json格式字符串的文件)
{
【姓名】:“Tsybius”,
【年齡】:23,
“sex_is_male”:沒錯,
“合作夥伴”:
{
"合作夥伴名稱":"加拉太亞",
" partner_age" :21,
“伴侶_性別_是_男性”:假
},
"成就":[ "ach1 "," ach2 "," ach3" ]
}
# include & ltiostream & gt
# include & ltfstream & gt
#包含" json/json.h "
使用命名空間std
int main()
{
JSON::Reader Reader;
JSON::Value root;
//從文件中讀取
ifstream為;
is.open( "PersonalInfo.json ",IOs::binary);
if (reader.parse(is,root))
{
//讀取根節點信息
string name = root[ "name" ]。asString();
int age = root[ "age" ]。asInt();
bool sex _ is _ male = root[" sex _ is _ male "]。as bool();
cout & lt& lt“我的名字是”& lt& lt名稱& lt& ltendl
cout & lt& lt“我是”& lt& lt年齡& lt& lt“歲”& lt& ltendl
cout & lt& lt“我是”& lt& lt(性別_是_男?《男人》:《女人》)& lt& ltendl
//讀取子節點信息
string partner _ name = root[" partner "][" partner _ name "]。asString();
int partner _ age = root[" partner "][" partner _ age "]。asInt();
bool partner _ sex _ is _ male = root[" partner "][" partner _ sex _ is _ male "]。as bool();
cout & lt& lt“我的合作夥伴的名字是”& lt& ltpartner _ name & lt& ltendl
cout & lt& lt(partner _ sex _ is _男性?“他”:“她”)& lt& lt"是"
& lt& ltpartner _ age & lt& lt“歲”& lt& ltendl
//讀取數組信息
cout & lt& lt“這是我的成就:“& lt& ltendl
for(int I = 0;我& lt根[“成就”]。size();i++)
{
string ach = root[“成就”][i]。asString();
cout & lt& ltach & lt& lt\ t ';
}
cout & lt& ltendl
cout & lt& lt“閱讀完成!”& lt& ltendl
}
is . close();
返回0;
}
生成文件
LIB =-L/usr/local/LIB/LIB JSON/-ljson _ Linux-gcc-4 . 4 . 7 _ libmt
答:a.o
g++ -o a -std=c++0x a.o $(LIB)
a . o . a . CPP
g++ -c a.cpp
清潔:
RM-RF a o a
第三,將信息保存為JSON格式。
a.cpp
# include & ltiostream & gt
# include & ltfstream & gt
#包含" json/json.h "
使用命名空間std
int main()
{
//根節點
JSON::Value root;
//根節點屬性
root[" name "]= Json::Value(" Tsybius ");
root[" age "]= Json::Value(23);
root[" sex _ is _ male "]= Json::Value(true);
//子節點
Json::價值合夥人;
//子節點屬性
partner[" partner _ name "]= Json::Value("加拉太亞");
partner[" partner _ age "]= Json::Value(21);
partner[" partner _ sex _ is _ male "]= Json::Value(false);
//子節點掛在根節點上。
root[" partner "]= Json::Value(partner);
//數組形式
根[“成就”]。append(" ach 1 ");
根[“成就”]。append(" ach 2 ");
根[“成就”]。append(" ach 3 ");
//直接輸出
cout & lt& ltfast writer:“& lt;& ltendl
JSON::fast writer fw;
cout & lt& ltfw . write(root)& lt;& ltendl & lt& ltendl
//縮進輸出
cout & lt& ltstyled writer:“& lt;& ltendl
JSON::styled writer SW;
cout & lt& ltsw.write(root)& ltendl & lt& ltendl
//輸出到文件
ofstream os
OS . open(" personal info ");
os & lt& ltSW . write(root);
OS . close();
返回0;
}
生成文件
LIB =-L/usr/local/LIB/LIB JSON/-ljson _ Linux-gcc-4 . 4 . 7 _ libmt
答:a.o
g++ -o a -std=c++0x a.o $(LIB)
a . o . a . CPP
g++ -c a.cpp
清潔:
RM-RF a o a
{
「成就」:[ "ach1 "," ach2 "," ach3" ],
【年齡】:23,
【姓名】:“Tsybius”,
“合作夥伴”:{
" partner_age" : 21,
"合作夥伴名稱":"加拉太亞",
“伴侶_性別_是_男性”:假
},
“sex_is_male”:真
}