Converting a list/array to JSON - performance issue.

  • Hi,

    I want to pass the PortalSearchItems list to be passed from qml to Cpp. Eventhough, I can get the result, app got just killed, when this code executes. I assume performance issue.

    Code Snippet: main.qml

    var varidPortalSearchItemsCount = idPortalSearchItems.totalResults;
    var vartextJson = "";
    for (var ii = 0; ii <= varidPortalSearchItemsCount -1 ;ii++)
    vartextJson += '{"title":"'+ idPortalSearchItems.results[ii].title +'"
    ,"owner":"'+ idPortalSearchItems.results[ii].owner +'"
    ,"url":"'+ idPortalSearchItems.results[ii].url +'"
    ,"itemId":"'+ idPortalSearchItems.results[ii].itemId +'"
    }' + ',';
    vartextJson = vartextJson.substring(0, vartextJson.length-1) + ']';
    vartextJson = '[' + vartextJson;
    var varJsonString =JSON.parse(vartextJson);


    Q_INVOKABLE bool eveWriteXML(const QJsonObject &pqvarPortalItemResult);


    bool HomeController::eveWriteXML(const QJsonObject &pqvarPortalItemResult)
    QVariantMap jsonQVariantMap = pqvarPortalItemResult.toVariantMap();
    for(QVariantMap::const_iterator iter = jsonQVariantMap.begin(); iter != jsonQVariantMap.end(); ++iter) {
    QVariant objQVariant = iter.key();
    QVariant objQVariant1 = iter.value();
    QString strTitleValue = objQVariant1.toMap().values("title").value(0).toSt ring();
    QString strUrlValue = objQVariant1.toMap().values("url").value(0).toStri ng();
    QString strOwnerValue = objQVariant1.toMap().values("owner").value(0).toSt ring();
    QString strItemIdValue = objQVariant1.toMap().values("itemId").value(0).toS tring();

    Thanks In advance.

  • Lifetime Qt Champion


    What does a run through the debugger tell you ?

  • Moderators

    Not sure about the performance issues but the way you are trying to extract JSON values seems to be wrong. iter.value() is going to return a QVariant of QString's because
    idPortalSearchItems.results[ii].title, idPortalSearchItems.results[ii].owner ... are all strings.
    So it should be replaced as:

    QVariant v = iter.value();
    QString strTitleValue = v.toString();

  • Re: Converting a list/array to JSON - performance issue.

    Hi p3c0,

    Thanks for Help.


Log in to reply

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