@lupnfer
2017-08-30T13:39:54.000000Z
字数 2279
阅读 757
Code
trans.json
{
"node": "IAS",
"name": "PeoCountingCfg",
"srcCfg": {
"vmInfo": {
"vmAddr": "192.168.0.1",
"vmUser": "loadmin",
"vmPasswd": "-42-49-66-1-242-227-212-197-182-238-222"
},
"camCode": "cam1"
},
"rule": {
"points": [
{
"point": [
100,
100
]
},
{
"point": [
100,
200
]
}
],
"direction": [
{
"pointStart": [
100,
100
]
},
{
"pointEnd": [
200,
100
]
}
],
"peopleCalib": [
{
"headY": 20,
"footY": 40
},
{
"headY": 80,
"footY": 120
},
{
"headY": 150,
"footY": 220
}
],
"number": {
"up": 20,
"down": 10
},
"factor": {
"up": 1.25,
"down": 0.76
}
}
}
video.json
{
"array" : {
"age" : 18,
"array1" : [
{
"key" : 0
},
{
"key" : 1
},
{
"key" : 2
},
{
"key" : 3
},
{
"key" : 4
},
{
"key" : 5
},
{
"key" : 6
},
{
"key" : 7
},
{
"key" : 8
},
{
"key" : 9
}
],
"map" : [
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
},
{
"arrayMap" : "zengraoli"
}
]
}
}
#include "iostream"
#include "string"
#include "json/json.h"
#include "fstream"
#include "assert.h"
using namespace std;
int ReadFromFile(const string& fileName)
{
ifstream fin;
fin.open(fileName.c_str());
assert(fin.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(fin, root, false))
{
printf("Read Json Error");
return -1;
}
// 获取数组中的数据
cout << "array data:" << endl;
int arraySize = root["array"]["map"].size();
for (int i = 0; i <arraySize; i++)
{
Json::Value valKeyValue = root["array"]["map"][i]["arrayMap"];
string tempKeyValue = valKeyValue.asString();
cout << "array map arrayMap:" << tempKeyValue << endl;
}
cout << endl;
cout << "my age is:" << root["array"]["age"] << endl;
int arraySize2 = root["array"]["array1"].size();
for (int i = 0; i <arraySize2; i++)
{
Json::Value valKeyValue = root["array"]["array1"][i]["key"];
int tempKeyValue = valKeyValue.asInt();
cout << "tempKeyValue:" << tempKeyValue << endl;
}
}
int WriteToFile(const string& fileName)
{
ofstream fout;
fout.open(fileName.c_str());
assert(fout.is_open());
Json::Value root;
Json::Value arrayObj;
Json::Value item;
Json::Value arrayMap;
for (int i = 0; i < 10; i++)
{
item["arrayMap"] = "zengraoli";
arrayMap.append(item);
}
root["array"]["map"]= arrayMap;
root["array"]["age"] = 18;
Json::Value item2;
for (int i = 0; i < 10; i++)
{
item2["key"] = i;
arrayObj.append(item2);
}
root["array"]["array1"] = arrayObj;
std::string out = root.toStyledString();
fout << root.toStyledString() << endl;
return 0;
}
int main(int argc, char* argv[])
{
//WriteToFile("video.json");
ReadFromFile("trans.json");
return 0;
}