Skip to content
  • 0 Votes
    4 Posts
    805 Views
    B

    @Mixlu
    What do you mean by "Unicode escaped format"?
    Normal characters won't be escaped, even a character like é also doesn't need to be escaped.
    If you use some special characters like \0, then it will be escaped to \u0000

  • 0 Votes
    3 Posts
    452 Views
    VRoninV

    You need to be more specific. xml doesn't have arrays, how should arrays be treated?

  • 0 Votes
    5 Posts
    621 Views
    QjayQ

    well this is how i have done it. Not sure if this is even the right way ( looks like a mess to me)

    QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); qDebug() << "doc :" << doc; QJsonObject doc_obj = doc.object(); qDebug() << "doc_obj :" << doc_obj; QJsonArray doc_array = doc_obj.value("results").toArray(); doc_obj = doc_array[0].toObject(); doc_array = doc_obj.value("series").toArray(); doc_obj = doc_array[0].toObject(); doc_array = doc_obj.value("values").toArray(); for(int i = 0; i < doc_array.size(); i++) { QJsonArray arr = doc_array[i].toArray(); qDebug() << "doc value : " << arr[1].toString(); }

    i am getting resuls though

  • 0 Votes
    3 Posts
    571 Views
    GPBetaG

    @VRonin Hi, thank you for the reply.

    The values are from XML files and there're some text values which types are not declared in the document but must be one of the valid JS values.

    I don't really like to use QString based methods to parse them manually, not just I don't know the exact type of a value, but also there're some details behind the JSON.parse(), such as extra spaces, precision problems, etc.

  • 0 Votes
    6 Posts
    1k Views
    QjayQ

    @Paul-Colby Thanks

    i myself did something like this :)

    QVariantMap json_map; const QJsonDocument doc = QJsonDocument::fromJson(output); qDebug().noquote() << doc; qDebug().noquote() << doc.array(); foreach (auto value, doc.array()) { qDebug().noquote() << value; qDebug().noquote() << value.toObject(); qDebug().noquote() << value.toObject().value("name"); json_map["name"] = value.toObject().value("name").toString(); json_map["installed"] = value.toObject().value("installed").toBool(); json_map["id"] = value.toObject().value("id").toString(); json_map["filename"] = value.toObject().value("filename").toString(); }
  • 0 Votes
    5 Posts
    5k Views
    tansgumusT

    Thank you guys.

    What a wonderful quick responses.

  • 1 Votes
    2 Posts
    4k Views
    D

    I found the reason for having this issue. I didn't store the result of fromJson function

  • Parsing JSON in QML

    Solved QML and Qt Quick
    3
    0 Votes
    3 Posts
    2k Views
    Stefan Monov76S

    @Wieland: Thanks!

  • 0 Votes
    12 Posts
    5k Views
    A

    @Gojir4
    Sure, Thanks for your Help.

  • 0 Votes
    7 Posts
    28k Views
    4

    @yeckel Thank you very much it's working now

    json file

    [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]

    Qt code

    QFile file; file.setFileName("test1.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError jsonError; QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError); if (jsonError.error != QJsonParseError::NoError){ qDebug() << jsonError.errorString(); } QList<QVariant> list = flowerJson.toVariant().toList(); QMap<QString, QVariant> map = list[0].toMap(); qDebug() << map["name"].toString();
  • 0 Votes
    4 Posts
    2k Views
    D

    @Joel-Bodenmann Alright.

  • 0 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    6 Posts
    2k Views
    D

    @Leonardo such a small but knotty thing...

  • 0 Votes
    3 Posts
    2k Views
    L

    I finally found a method to solve the problem. The following is the code snippet.

    main.cpp

    #include <QGuiApplication> #include <QStringList> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> #include <QtQuick/qquickitem.h> #include <QtQuick/qquickview.h> #include <jsondata.h> int main(int argc, char ** argv) { QGuiApplication app(argc, argv); QStringList datalist; Jsondata jsondata; jsondata.datalistmethod(); datalist = jsondata.datalist; QQuickView view; QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(datalist)); view.setSource(QUrl("qrc:main.qml")); view.show(); return app.exec(); }

    jsondata.h

    #ifndef JSONDATA_H #define JSONDATA_H #include <QObject> #include <QNetworkReply> #include <QStringList> #include <QNetworkAccessManager> class Jsondata : public QObject { Q_OBJECT public: QStringList datalist; explicit Jsondata(QObject *parent =0); void datalistmethod(); public slots: void onResult (QNetworkReply*); private: QNetworkAccessManager *manager; };

    jsondata.cpp

    #include "jsondata.h" #include <QNetworkReply> #include <QNetworkRequest> #include <QNetworkAccessManager> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QEventLoop> #include <QtQuick/qquickitem.h> #include <QtQuick/qquickview.h> #include <QTimer> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> Jsondata::Jsondata(QObject *parent) : QObject(parent) { } void Jsondata::datalistmethod() { // Now parse this JSON according to your needs ! manager = new QNetworkAccessManager(this); manager->setNetworkAccessible(QNetworkAccessManager::Accessible); QNetworkRequest request; QEventLoop eventloop; QUrl url("http://***/api/web/v1/links"); request.setUrl(url); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(finished()), &eventloop, SLOT(quit())); eventloop.exec(); onResult(reply); } void Jsondata::onResult(QNetworkReply* reply) { QString data = (QString) reply->readAll(); qDebug() << "Response:" << data; QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8()); QJsonArray jsonArray = jsonResponse.array(); foreach (const QJsonValue & value, jsonArray) { QJsonObject obj = value.toObject(); datalist.append(obj["name"].toString()); datalist.append(obj["link"].toString()); } }

    By adding event loop, the datalist method will wait for the onResult method completed to execute next line of code. This is using the example of string list model. If anyone feel it useful for reference, please +1 my post. Thank you. ^^

  • 0 Votes
    2 Posts
    5k Views
    ?

    Hi @LuisAbreu!

    I cannot confirm this. Are you sure your json file is valid? The following works for me:

    ============================================
    Json file:

    { "className": "ShoppingCart", "closedOn": "Fri Mar 27 15:52:13 2015", "coupon_count": "0", "createdOn": "Fri Mar 27 15:51:09 2015", "list": [ { "_className": "Product", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "5601151333457", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": -1, "min_age": 0, "name": "Sumo Compal Cl<C3><A1>ssico Tutti-Fruti 1lt", "userCreated": "0", "weight": "0" }, { "_className": "Product", "fromyoubeep": "0", "has_alarm": 0, "id": "", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "userCreated": "0", "weight": "0" } ], "loyaltyCard": { "_className": "LoyaltyCard", "barcode": "2446037038353", "barcodeType": "EAN13", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "2446037038353", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": 0, "min_age": 0, "name": "Cart<C3><A3>o Poupa Mais", "price": "0", "product_id": "-1", "quantity": "0", "quantityValidated": "0", "state": "0", "type": 1, "userCreated": "0", "weight": "0" }, "mobile_checkout": "1" }

    ============================================
    C++ code:

    QFile file("/home/pw/file.json"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "file error"; return; } const QByteArray ba = file.readAll(); file.close(); QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(ba, &err); qDebug() << err.errorString(); qDebug() << err.offset; QJsonObject sett2 = doc.object(); qDebug() << sett2.isEmpty(); QJsonObject sett3 = sett2.value(QString("loyaltyCard")).toObject(); qDebug() << sett3.isEmpty(); QJsonValue sett4 = sett3.value(QString("barcode")); qDebug() << sett4.toString();

    ============================================
    Cheers!