Json数据结构:
1.对象 { }
2.数值 [ ]
JSON 值可以是:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在方括号中)
对象(在花括号中)
null
文件读取:
int ParseJsonFromFile(const char* filename)
{
Reader reader;
Value root;
ifstream is(filename);
if (!is.is_open())
cout << "not open" << endl;
if (reader.parse(is,root))
{
string szTxt;
int nSize = root["people"].size();
for (int i = 0; i < nSize; ++i)
{
string image1 = root["people"][i]["firstName"].asString();
string image2 = root["people"][i]["lastName"].asString();
string image3 = root["people"][i]["email"].asString();
cout << image1 << " " << image2 << " " << image3 << endl;
}
}
return 0;
}
文件:
{
"people":[
{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
{"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
{"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
]
}