JSON Parse



  • Hello everyone,

    I'm trying to get used with QJson so I want to solve the example above:

    @{
    "spatialReference": {
    "wkid": 4326,
    "latestWkid": 4326
    },
    "locations": [
    {
    "name": "Bucharest, Bucureşti, Romania",
    "extent": {
    "xmin": 25.966258,
    "ymin": 44.292248000000001,
    "xmax": 26.246258000000001,
    "ymax": 44.572248000000002
    },
    "feature": {
    "geometry": {
    "x": 26.106257802000471,
    "y": 44.432248090000485
    },
    "attributes": {
    "Score": 100,
    "Addr_Type": "POI"
    }
    }
    }
    ]
    }
    @

    How can I get the "x" value? I'm using Qt 5.4 and QJsonDocument. For example, the "name" value I was successfully able to read but for now I have no idea how to do with "x" or "y". I suppose I still don't know how to read the "tree" structure.


  • Moderators

    Hi,

    Can you show us the code you used to extract "name"?

    Basically,
    @
    // Instead of this:
    locationObject["name"].toString();

    // Call this:
    locationObject["feature"].toObject();
    @

    ...and then do the same for the inner nodes.



  • Hey,

    Now I just figure it out. Thanks :)

    However I will show the code in case there is anyone with the same problem. Maybe there is another more elegant way to get to the "x", "y" values and read it but for the moment this one is working:

    @QJsonObject jsonObj = jsonResponse.object();
    QJsonValue value=jsonObj.value("locations");
    QJsonArray array=value.toArray();

    foreach(const QJsonValue & v, array)
    { qDebug() << "Name:" << v.toObject().value("name").toString();
    qDebug() << "Longitude:" <<v.toObject().value("feature").toObject().value("geometry").toObject().value("x").toDouble();
    qDebug() << "Latitude:" <<v.toObject().value("feature").toObject().value("geometry").toObject().value("y").toDouble();
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.