[英]How do I parse an array of objects using nlohmann json?
I have looked through many answers using nlohmann json but I have not found one that works with the below json.我已经使用 nlohmann json 查看了许多答案,但我还没有找到一个适用于以下 json 的答案。
json: json:
[{
"geometry":
{
"type": "dd",
"coordinates":
[
[
[
-156.09375,
19.311143355064644
],
[
-156.09375,
19.973348786110606
],
[
-155.390625,
19.973348786110606
],
[
-155.390625,
19.311143355064644
],
[
-156.09375,
19.311143355064644
]
]
]
},
"type": "fdgdsg",
"properties":
{},
"layer":
{
"id": "bounding_box",
"type": "fill",
"source": "bounding_box_source",
"paint":
{
"fill-color":
{
"r": 0,
"g": 0.5333333333333333,
"b": 0.5333333333333333,
"a": 1
},
"fill-opacity": 0.45
},
"layout":
{}
},
"source": "bounding_box_source",
"state":
{}
},
{
"geometry":
{
"type": "yyy",
"coordinates":
[
[
[
-155.56640625,
19.973348786110606
],
[
-155.390625,
19.973348786110606
],
[
-155.390625,
19.311143355064644
],
[
-155.56640625,
19.311143355064644
],
[
-155.56640625,
19.973348786110606
]
]
]
},
"type": "Feature",
"properties":
{},
"layer":
{
"id": "bounding_box",
"type": "fill",
"source": "bounding_box_source",
"paint":
{
"fill-color":
{
"r": 0,
"g": 0.5333333333333333,
"b": 0.5333333333333333,
"a": 1
},
"fill-opacity": 0.45
},
"layout":
{}
},
"source": "bounding_box_source",
"state":
{}
}
]
I am using我在用
for (auto& el : j.items())
{
std::cout << el.value() << '\n'
}
This gets the geometry value of all of the objects but how do I access the other fields besides geometry such as layer or properties for each object?这将获取所有对象的几何值,但是如何访问除几何之外的其他字段,例如每个对象的图层或属性?
I figured it out I was not setting el.value to it's own object.我发现我没有将 el.value 设置为它自己的对象。
Full parsing code:完整解析代码:
json j;
ifstream ifs(*File);
if (!ifs.is_open())
{
//return false;;
}
ifs >> j;
ifs.close();
for (auto& elm : j.items())
{
nlohmann::json object = elm.value();
std::cout << object.at("geometry") << std::endl;
std::cout << object.at("geometry")["type"] << std::endl;
std::cout << object.at("source") << std::endl;
}
So object now represents each top-level unnamed object.所以 object 现在代表每个顶级未命名对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.