JSon Parsing



  • Hello.

    I developing a application that makes a request to an server and i get the following response (https://octopart.com/api/v3/parts/match?apikey=EXAMPLE_KEY&queries=[{"mpn":"SN74S74N"}]&pretty_print=true&include[]=specs&include[]=specs). I need to access the variable "results">"specs">"case_package">"value". This is my code:

    void MainWindow::octopartData(QByteArray data)
    {
        QString dataString;
        dataString = data;
        // ui->textBrowser->append(data); //DEBUG PURPOSES
        QJsonDocument temp = QJsonDocument::fromJson(data);
        QJsonObject jObject = temp.object();
        QVariantMap mainMap = jObject.toVariantMap();
        QVariantMap results = mainMap["results"].toMap();
        QVariantMap specs = results["specs"].toMap();
        QVariantMap case_package = specs["case_package"].toMap();
        qDebug()<<case_package["value"];
    }
    

    But is not working, debug is showing only QVariant(Invalid). I would like to get some advice to solve this problem.

    Thanks in advance.



  • use toObject instead of toVariantMap


  • Qt Champions 2016

    Hi
    Why you need to convert to
    Object.toVariantMap();

    If you take

      qDebug() << jObject["message"] <<  jObject["__class__"];
    

    Its not the values?

    See small sample here
    https://forum.qt.io/topic/74715/work-with-json-format/3

    Its parsing
    "[ { "Name" : "Bob", "Phonenumber" : 123 },
    { "Name" : "Alice", "Phonenumber" : 321 } ]"

    so almost the same :)



  • @mrjj said in JSon Parsing:

    Hi
    Why you need to convert to
    Object.toVariantMap();

    If you take

      qDebug() << jObject["message"] <<  jObject["__class__"];
    

    Its not the values?

    See small sample here
    https://forum.qt.io/topic/74715/work-with-json-format/3

    Its parsing
    "[ { "Name" : "Bob", "Phonenumber" : 123 },
    { "Name" : "Alice", "Phonenumber" : 321 } ]"

    so almost the same :)

    Hi.

    I'm converting it to VariantMap because i was following this http://blog.linux4us.org/2013/09/05/parse-json-with-qjson-in-qt5/. The data i have is like the following: https://paste2.org/yIBBGm8p

    I think the difference between my data and the example you posted is that in my data the value i want to read ("value") is "inside" "case_package", that is inside "specs" that is inside "results".



  • Edit2

    I corrected the code. You did not realise some of the values were arrays and that there was an "items" object in-between.

    You can use tools like http://jsonviewer.stack.hu/ to have a clearer view of what you are looking at

    void MainWindow::octopartData(QByteArray data)
    {
        const QJsonDocument temp = QJsonDocument::fromJson(data);
        const QJsonObject jObject = temp.object();
        const QJsonArray results = jObject["results"].toArray();
    
        for(const QJsonValue& res : results){
            const QJsonObject result= res.toObject();
            const QJsonArray items = result["items"].toArray();
            for(const QJsonValue& ite : items){
                const QJsonObject item= ite.toObject();
                const QJsonObject specs = item["specs"].toObject();
                const QJsonObject case_package = specs["case_package"].toObject();
                const QJsonArray value = case_package["value"].toArray();
                for(auto& val : value){
                    qDebug() << val;
                }
            }
        }
    }
    


  • @VRonin said in JSon Parsing:

    Edit2

    I corrected the code. You did not realise some of the values were arrays and that there was an "items" object in-between.

    You can use tools like http://jsonviewer.stack.hu/ to have a clearer view of what you are looking at

    void MainWindow::octopartData(QByteArray data)
    {
        const QJsonDocument temp = QJsonDocument::fromJson(data);
        const QJsonObject jObject = temp.object();
        const QJsonArray results = jObject["results"].toArray();
    
        for(const QJsonValue& res : results){
            const QJsonObject result= res.toObject();
            const QJsonArray items = result["items"].toArray();
            for(const QJsonValue& ite : items){
                const QJsonObject item= ite.toObject();
                const QJsonObject specs = item["specs"].toObject();
                const QJsonObject case_package = specs["case_package"].toObject();
                const QJsonArray value = case_package["value"].toArray();
                for(auto& val : value){
                    qDebug() << val;
                }
            }
        }
    }
    

    Hello.

    Thanks, it's working now. Sorry for the newbie question, but what the 3 '"fors" are doing exactly?

    And just one more question, if inside "items" i have "0" and "1", how can i select one of these itens?



  • the answers are related:

    but what the 3 '"fors" are doing exactly?

    they iterate over every item of the array

    if inside "items" i have "0" and "1", how can i select one of these itens?

    instead of iterating over all of them (with the for) you can use at() to select a specific one, for example: const QJsonObject item= items.at(0).toObject();



  • Thanks a lot, @VRonin.



  • Sorry, for "reviving" the topic, but i have one more doubt.

    I'm trying to acces the property "mpn" from "itens" using the following code:

    void MainWindow::octopartData(QByteArray data)
    {
        const QJsonDocument temp = QJsonDocument::fromJson(data);
        const QJsonObject jObject = temp.object();
        const QJsonArray results = jObject["results"].toArray();
        for(const QJsonValue& res : results)
    {
            const QJsonObject result= res.toObject();
            const QJsonArray items = result["items"].toArray();
            const QJsonObject item = items.at(0).toObject();
            const QJsonArray mpn = item["mpn"].toArray();
            qDebug() << mpn;
        }
    }
    

    But i only get an empty array. Did i did something wrong when accessing the data?


  • Qt Champions 2016

    Hi
    "mpn": "SN74S74N",
    Do not look like array to me.
    Looks like a value.
    http://doc.qt.io/qt-5/qjsonvalue.html#details
    "The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. "

    so try the type() and see what you got :)



  • @mrjj said in JSon Parsing:

    Hi
    "mpn": "SN74S74N",
    Do not look like array to me.
    Looks like a value.
    http://doc.qt.io/qt-5/qjsonvalue.html#details
    "The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. "

    so try the type() and see what you got :)

    It was a string after all. Now my code is working as bellow:

    void MainWindow::octopartData(QByteArray data)
    {
        const QJsonDocument temp = QJsonDocument::fromJson(data);
        const QJsonObject jObject = temp.object();
        const QJsonArray results = jObject["results"].toArray();
        for(const QJsonValue& res : results)
    {
            const QJsonObject result= res.toObject();
            const QJsonArray items = result["items"].toArray();
            const QJsonObject item = items.at(0).toObject();
            const QString mpn = item["mpn"].toString();
            qDebug() << mpn;
        }
    }
    

    Thanks a lot for the help.


Log in to reply
 

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