I tried to load the json file by incorporating picojson into VisualStudio 2012 C++, but I was not able to load the json file successfully.
(The data I want most is multiple two-dimensional arrays in the "paths" array in the json file.)
In the simple first tier, it seems to have been built and read even when executed.
The parameters in the array structure are read in the middle, and it is not working.
source code for loading
// Variables for loading files
std::ifstream fs;
// load a file
fs.open("sample.json", std::ios::binary);
// read check
// Error without data in fs variable
assert(fs);
// Load into Picojson
picojson::value value;
fs>>val;
// Closed because fs variable is no longer used
fs.close();
// Get Player Name
picojson::object&obj=val.get<picojson::object>();
picojson::array&feat=obj["features"].get<picojson::array>();//★ Read up to this point
The json file you want to read (sample.json)
{
"displayFieldName":",
"geoType": "geotype01",
"features": [
{
"attributes": {
"FID": 0,
"prop0": "123456"
},
"param": {
"paths": [
[
[
100.123,
23.456
],
[
123.456,
34.567
],
[
135.790,
45.678
],
[
111.222,
56.789
]
]
]
}
},
{
"attributes": {
"FID": 1,
"prop0": "124816"
},
"param": {
"paths": [
[
[
123.456,
98.876
],
[
234.567,
87.654
],
[
345.678,
76.543
],
[
456.789,
65.432
]
]
]
}
},
],
"fieldAliases": {
"FID": "FID",
"prop0": "prop0"
},
"fields": [
{
"alias": "FID",
"name": "FID",
"type": "FieldTypeAAA"
},
{
"alias": "prop0",
"length"—254,
"name": "prop0",
"type": "FieldTypeString"
}
],
"specialReference": {
"latestWkid":4321,
"wkid":4321
}
}
As shown in , there are many tiers, so I'm having trouble knowing how to read them.
(There are a lot of picojson introduction sites, but I'm troubled because I can't find a site that has a hierarchical description like a sample.)
There are multiple arrays with the tag "feature" (two here), so I read them in the following description.
picojson::array&feat=obj["features"].get<picojson::array>();;
However, after that, I don't know how to read the data with various tags in the array, so I'm stuck.
//features
for(inti=0;i<feat.size();i++){
// attributes
string hoge=obj["attributes"].get<string>();
、、、
As shown in , I thought that only the array of "features" would be read in a loop, but it didn't work.
Supplementary information (such as FW/Tool version)
As I mentioned earlier,
·I would like to know how to describe it when it has a hierarchical structure such as sample.json.son.
"·The data I want the most is multiple 2D arrays in the ""paths"" array, and I would like to store them in vector type."
·I'm trying picojson this time, but I'd appreciate it if you could let me know if there are any other good ones.
If anyone knows, I would appreciate it if you could give me some tips and know-how to describe it.
I would appreciate it if you could let me know.
Before you answer, JSON Pretty Linter Ver3 or Best JSON Formatter and JSON Validator: Online JSON Validator multiplied by the line .
The contents of JSON are objects and arrays, so you will need to steadily access the inside according to the structure as follows:
There may be a better way to do it (e.g., spin the array in a different way, not an index), but for now, you can do it like this.
Modify:
"The data I want the most is multiple two-dimensional arrays in the ""paths"" array, and I want to store them in vector type.""""Multiple 2D arrays" will be stored as a 3D array.
class feature {
public:
double FID;
std::string prop0;
std::vector<std::vector<std::vector<double>>paths;
};
"After ""// ★ Read this far"", do the following:Output processing is for verification purposes only.
std::vector<feature>features(feat.size());
// circulate a few features
for(unsigned inti=0;i<feat.size();i++){
// attributes
// string hoge = obj ["attributes"].get<string>();
picojson::object&featN=feat[i].get<picojson::object>();;
picojson::object&attr=featN["attributes"].get<picojson::object>();
features[i].FID=attr["FID"].get<double>();
features[i].prop0=attr["prop0"].get<std::string>();
std::cout<<"features:"<<i<<", FID:"<<features[i].FID<<", prop0:"<features[i].prop0<<std::::endl;
picojson::object¶m=featN["param"].get<picojson::object>();
picojson::array&paths=param["paths"].get<picojson::array>();
features[i].paths.resize(paths.size()); // only 1 in the sample
for(unsigned int j=0;j<paths.size();j++){
picojson::array&pathD2=paths[j].get<picojson::array>();
features[i].paths[j].resize(pathD2.size())); // Only 4 in the sample
for(unsigned intk=0;k<pathD2.size();k++){
picojson::array&pathD3=pathD2[k].get<picojson::array>();
features[i].paths[j][k].resize(pathD3.size())); // Only 2 in the sample
for(unsigned intl=0;l<pathD3.size();l++){
features[i].paths[j][k][l]=pathD3[l].get<double>();
std::cout<<"paths1stIdx:"<<j<<",2ndIdx:"<k<<",3rdIdx:"<<<<<<<features[].[jd];];[jd]
}
}
}
}
© 2024 OneMinuteCode. All rights reserved.