I want picojson to read hierarchical json files

Asked 2 years ago, Updated 2 years ago, 76 views

I tried to load the json file with picojson in VisualStudio 2012 C++, but I couldn't load the json file as intended.
(The data I want most is the array in the "polygonList" array in the json file (pointId, X, Y)

I tried to read the question I asked you before, but after reading "field" in array type
I tried to read PointA, PointB, ... and array polygonList, but it didn't work.

The json file you want to read

{
    "field": [
        {
             "PointA": "10",
             "PointB": "12",
             "fieldId": "12345",
             "polygonList": [
                 {
                     "pointId": "0",
                     "X": "36.4",
                     "Y"—"137.8"
                 },
                 {
                     "pointId": "1",
                     "X": "36.4",
                     "Y"—"137.8"
                 },
                 {
                     "pointId": "2",
                     "X": "36.4",
                     "Y"—"137.8"
                 },
                 {
                     "pointId": "3",
                     "X": "36.4",
                     "Y"—"137.8"
                 }
             ]
        }
    ]
}

Tried

The source code part you were creating

// 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["field"].get<picojson::array>();

In , I got the field range as array type and

for(unsigned inti=0;i<feat.size();i++){

As a result, I was trying to have it read for multiple fields.However,
as per the answers I referred to I tried to make each array an object and convert it to read the value of each tag.

picojson::object&featN=feat[i].get<picojson::object>();;
double hoge=featN["PointA"].get<double>();

I tried to write that, but an error did not pass. (There is an error in the get() line.)
About array type coordinate values

picojson::array&pathD2=paths["polygonList"].get<picojson::array>();;
for(unsigned intk=0;k<pathD2.size();k++){
        double id = pathD2 ["pointId"].get<double>();
        doublex=pathD2["X"].get<double>();
        doubley=pathD2["Y"].get<double>();
}

There was an error when I wrote

and I was unable to retrieve it.

The process is almost the same as the one you asked me before, so I thought it would work, but it doesn't work well.

Is there any mistake in the description method?
Please let me know if there are any errors in the description method.

c++ json array data-structure

2022-09-30 14:47

1 Answers

Unlike before, the value part is surrounded by ", so you can use std::string to get and convert it to stod or strtod.
The following is true:

for(unsigned inti=0;i<feat.size();i++){
    picojson::object&featN=feat[i].get<picojson::object>();;

    doublepointA=std::stod(featN["PointA"].get<std::string>());
    doublepointB=std::stod(featN["PointB"].get<std::string>());
    std::string fieldId=featN["fieldId"].get<std::string>();
    std::cout<<"PointA:"<<<pointA<", PointB:"<<pointB<<", fieldId:"<fieldId<<<std:::endl;;

    picojson::array&pgList=featN["polygonList"].get<picojson::array>();// variable name changed
    for(unsigned intk=0;k<pgList.size();k++){
        
        picojson::object&pg=pgList[k].get<picojson::object>();
        
        double id = std::stod(pg["pointId"].get<std::string>());
        doublex=std::stod(pg["X"].get<std::string>());
        doubley=std::stod(pg["Y"].get<std::string>());
        std::cout<<"pointId:"<<id<<", X:"<<x<<", Y:"<<y<<std:::::endl;
    }
}


2022-09-30 14:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.