JSON help



  • Hi guys... I have been trying for some time to get particular "fields" from this google maps API JSON request. I have successfully been able to get one of the parent results ( formatted_address ) but I would rather be able to get the "long_name" child of address_components. Could somebody give me a little hand with this please?

    google maps API JSON

       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "DH5 9QW",
                   "short_name" : "DH5 9QW",
                   "types" : [ "postal_code" ]
                },
                {
                   "long_name" : "School Road",
                   "short_name" : "School Rd",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "East Rainton",
                   "short_name" : "East Rainton",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Houghton le Spring",
                   "short_name" : "Houghton le Spring",
                   "types" : [ "postal_town" ]
                },
                {
                   "long_name" : "Tyne and Wear",
                   "short_name" : "Tyne and Wear",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "England",
                   "short_name" : "England",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "United Kingdom",
                   "short_name" : "GB",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "School Rd, East Rainton, Houghton le Spring DH5 9QW, UK",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 54.8226881,
                      "lng" : -1.4832795
                   },
                   "southwest" : {
                      "lat" : 54.8216359,
                      "lng" : -1.4868377
                   }
                },
                "location" : {
                   "lat" : 54.8221041,
                   "lng" : -1.4841939
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 54.82351098029149,
                      "lng" : -1.4832795
                   },
                   "southwest" : {
                      "lat" : 54.8208130197085,
                      "lng" : -1.4868377
                   }
                }
             },
             "place_id" : "ChIJew6BECdjfkgR8NnVCEXnCjs",
             "types" : [ "postal_code" ]
          }
       ],
       "status" : "OK"
    }
    

    Qt code

    void MainWindow::replyFinished(QNetworkReply* reply)
    {
    
    
       QString strReply = (QString)reply->readAll();
    
       QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    
       QJsonObject jsonroot = jsonResponse.object();
       QJsonValue value = jsonroot.value("results");
    
       QJsonArray array = value.toArray();
    
    
    QStringList addresses;
    QStringList addresses2;
    
    foreach (const QJsonValue & v, array)
    {
    QJsonObject obj = v.toObject();
    QJsonValue val = obj.value("address_components").toString();
    qDebug() << "data" <<val << endl;
    
    QJsonObject item = val.toObject();
    QJsonValue subobj = item.value("long_name");
    QString str = subobj.toString();
    qDebug() << "long_name" <<str << endl;
    
    
    
    addresses << obj.value("formatted_address").toString();
    }
    


  • Hi @Scottish_Jason,

    There were two problems with the code.

    First, this line:

    QJsonValue val = obj.value("address_components").toString();
    

    Should not have ended with .toString() ... since address_components is a JSON array, QJsonValue::toString will return a null QString.

    Second, this line:

    QJsonObject item = val.toObject();
    

    Since item is a JSON array (assuming the first issue above was fixed), QJsonValue::toObject will return an empty QJsonObject.

    So a working (though only seeing one long_name) version of your code would be:

    QJsonObject obj = v.toObject();
    qDebug() << "OBJECT"  << obj;
    QJsonValue val = obj.value("address_components"); //< This line changed.
    qDebug() << "data" <<val << endl;
    
    QJsonObject item = val.toArray().at(0).toObject(); //< This line changed.
    qDebug() << "ITEM" << item;
    QJsonValue subobj = item.value("long_name");
    QString str = subobj.toString();
    qDebug() << "long_name" <<str << endl;
    

    To get all of the long_name values, loop though them too. Here's how I'd do the whole thing:

    foreach (const QJsonValue &item, jsonResponse.object().value("results").toArray())
    {
        const QJsonObject address = item.toObject();
        foreach (const QJsonValue &component, address.value("address_components").toArray())
        {
            qDebug() << component.toObject().value("long_name");
        }
        qDebug() << address.value("formatted_address");
    }
    

    Of course that could do with some error checking too.

    Cheers.



  • It's hard to be certain without a complete function, but there appears to be a level of structure missing in the code.
    From the JSON, I see:

    object
        array of objects // results
            array of objects // address_components
                string // long_name
    

    The code snippet is missing the second array.

    #include <QDebug>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>
    #include <QFile>
    
    int main(int argc, char **argv)
    {
        QFile file(argv[1]);
        file.open(QFile::ReadOnly);
        QJsonDocument jsonResponse = QJsonDocument::fromJson(file.readAll());
        QJsonObject jsonroot = jsonResponse.object();
        QJsonValue value = jsonroot.value("results");
        QJsonArray array = value.toArray();
        QStringList addresses;
            
        for (const QJsonValue & v: array) {
            QJsonArray address_components =  v.toObject().value("address_components").toArray();
            for (const QJsonValue & name : address_components) {
                QJsonValue long_name = name.toObject().value("long_name");
                addresses << long_name.toString();
            }
        }
            
        qDebug() << addresses;
        return 0;
    }
    

    output: ("DH5 9QW", "School Road", "East Rainton", "Houghton le Spring", "Tyne and Wear", "England", "United Kingdom")



  • Thanks guys. These posts helped me solve my issue.


Log in to reply
 

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