123.json配置文件如下:
{
"ID": 20,
"longitude": 112.12345,
"latitude": 28.67890,
"num": 123,
"openFlag": true,
"name": "xi xi",
"data": [4, 2, 3, 4, 5]
}
序列化代码如下:
#include "../common.h"
#include <jsoncpp/json/json.h>
int main()
{
ifstream readFile("123.json",ios::binary);
if(!readFile.is_open())
{
printf("open fail\n");
return -1;
}
else
{
Json::Reader reader;
Json::Value root;
if(reader.parse(readFile,root)) //reader将文件中的内容解析到root
{
cout << "ID: " << root["ID"].asInt() << endl;
cout << "longitude: " << root["longitude"].asFloat() << endl;
cout << "latitude: " << root["latitude"].asFloat() << endl;
cout << "openFlag: " << root["openFlag"].asBool() << endl;
cout << "name: " << root["name"].asString() << endl;
for(int i = 0; i < root["data"].size(); ++i)
{
printf("data[%d]: %d\n",i,root["data"][i].asInt());
}
}
else
{
cout << "no reader" << endl;
}
readFile.close();
return 0;
}
}
编译的时候需要连接 jsoncpp:
g++ 1.cpp -ljsoncpp -std=c++11
结果如下:
ID: 20
longitude: 112.123
latitude: 28.6789
openFlag: 1
name: xi xi
data[0]: 4
data[1]: 2
data[2]: 3
data[3]: 4
data[4]: 5
稍微复杂一点的也是如此,实际上C++使用json与使用数组的方式差不多:
#include "../common.h"
int main()
{
ifstream readFile("456.json",ios::binary);
if(!readFile.is_open())
{
printf("open fail\n");
return -1;
}
else
{
Json::Reader reader;
Json::Value root;
if(reader.parse(readFile,root)) //reader将文件中的内容解析到root
{
cout << "devMessage_UUID: " << root["devMessage"]["UUID"].asString() << endl;
cout << "devMessage_stationName: " << root["devMessage"]["stationName"].asString() << endl;
cout << "devMessage_longitude: " << root["devMessage"]["longitude"].asFloat() << endl;
cout << "devMessage_latitude: " << root["devMessage"]["latitude"].asFloat() << endl;
cout << "devMessage_AntennaShape: " << root["devMessage"]["AntennaShape"].asInt() << endl;
cout << "devMessage_TimeZone: " << root["devMessage"]["TimeZone"].asInt() << endl;
cout << endl;
cout << "softConfig_autoStart: " << root["softConfig"]["autoStart"].asBool() << endl;
cout << "softConfig_autoRest: " << root["softConfig"]["autoRest"].asBool() << endl;
cout << "softConfig_initPhase433: [";
for(int i = 0; i < root["softConfig"]["initPhase433"].size(); ++i)
{
if(i == root["softConfig"]["initPhase433"].size() - 1)
{
cout << root["softConfig"]["initPhase433"][i].asFloat() << "]" << endl;
}
else
{
cout << root["softConfig"]["initPhase433"][i].asFloat() << " ,";
}
}
cout << "softConfig_d800: " << root["softConfig"]["d800"].asFloat() << endl;
}
else
{
cout << "no reader" << endl;
}
readFile.close();
return 0;
}
}
456.json:
{
"devMessage": {
"UUID": "json",
"stationName": "station",
"longitude": 112.123456,
"latitude": 28.789012,
"AntennaShape": 3,
"TimeZone": 8
},
"softConfig": {
"autoStart": true,
"autoRest": true,
"initPhase433": [-0.9, -0.8, -1.9, -2.9],
"d800": 0.15
}
}
结果如下:
devMessage_UUID: json
devMessage_stationName: station
devMessage_longitude: 112.123
devMessage_latitude: 28.789
devMessage_AntennaShape: 3
devMessage_TimeZone: 8
softConfig_autoStart: 1
softConfig_autoRest: 1
softConfig_initPhase433: [-0.9 ,-0.8 ,-1.9 ,-2.9]
softConfig_d800: 0.15