如何将GeoJson文件的所有字符串转换为类对象?
GeoJson文件示例:
{"type":"FeatureCollection","features" :[
{"type":"feature","properties":{"type":"126"},"geometry":
{"type":"POINT","coordinates":[197,98]}}
{"type":"feature","properties":{"type":"126"},"geometry":
{"type":"POINT","coordinates":[142,24]}}
{"type":"feature","properties":{"type":"126"},"geometry":
{"type":"POINT","coordinates":[185,53]}}]}
我需要为每个字符串创建一个类对象并填写其字段:
class Json
{
public:
Json();
QString type;
QString propeties_type;
QString geometry_type;
int x_coord,y_coord;
};
我尝试使用QVariant,但我不知道如何提取信息:
QByteArray data = file.readAll();
QJsonDocument jsdoc = QJsonDocument::fromJson(data);
QVariant variant = jsdoc.object()["features"].toVariant();
答案 0 :(得分:0)
我会按以下方式进行:
std::vector<Json> result;
QByteArray data = file.readAll();
QJsonDocument jsdoc = QJsonDocument::fromJson(data);
QJsonArray features = jsdoc.object()["features"].toArray();
QJsonArray::const_iterator it = features.constBegin();
while (it != features.constEnd())
{
Json json;
QJsonObject feature = it->toObject();
json.type = "feature";
json.propeties_type = feature["properties"].toObject()["type"].toString();
QJsonObject geometry = feature["geometry"].toObject();
json.geometry_type = geometry["type"].toString();
QJsonArray coordinates = geometry["coordinates"].toArray();
assert(coordinates.size() == 2);
json.x_coord = coordinates.at(0).toInt();
json.x_coord = coordinates.at(1).toInt();
result.push_back(json);
++it;
}
代码生成Json
个对象的向量,每个对象都包含有关“特征”的信息。