Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Parsing Json with QML

Parsing Json with QML

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 7 Posters 34.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    situ117
    wrote on 8 May 2011, 21:29 last edited by
    #1

    Hi,

    As QML does not have any Json model, what remains the best way to parse Json data ? I had a look at couple of alternatives. Some of which previously explained here:

    http://developer.qt.nokia.com/forums/viewthread/2057

    But not sure about which one to use. Right now I'm using raw Javascript code to fetch and parse Json, and set properties (instead of creating models). I'm not willing to mess up with C++ code right now. What's the best way to parse Json in QML ? Is there any progress from Nokia in this direction ?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on 9 May 2011, 07:35 last edited by
      #2

      I would still recommend qjson. Even though it is C++ it is so easy to use that it is silly ;-)

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 9 May 2011, 07:36 last edited by
        #3

        Not sure. I guess it depends what you are looking for. Do you need some kind of model implementation that works based on JSON so you can use it with your views? That is actually not a bad idea, IMHO. Something like QXmlListModel, but for JSON?

        Edit:
        I am not the first one to think of this, obviously. There is an existing "suggestion":http://bugreports.qt.nokia.com/browse/QTBUG-12117 in the bugtracker. You might want to vote for that.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          situ117
          wrote on 9 May 2011, 09:08 last edited by
          #4

          Hi,

          Done voting !!! Right now I'm not doing any complex and repetitive operation on json data, so simple javascript won't hurt, but as complexity of data increases, I thought a model would be better idea. I think I would have to introduce C++ code in my application. Any sample code for how to create QXmlListModel like model for Json. (except the code shown in bug tracker)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dridk
            wrote on 21 Jun 2011, 14:57 last edited by
            #5

            Hello here is my point of view! I tested everything! XMLModel, javascript XML parser, C++ side parser etc...

            Do Not use C++ Json parser : Because exchange data between c++ and QML is slow

            Make a Javascript parser ! It's more easy with eval ! Here is an example :

            Json data
            @
            {"index":["all"],"flux":{"all":[{"data":{"title":"boris","icon":"icon.png"}]}}
            @

            QML side : main.qml
            @
            import QtQuick 1.0
            import "parser.js" as JS

            Item {
            id:root
            width: 360
            height: 640

            Component.onCompleted: JS.load()
            
            
            ListModel {  id:listModel }
            
            ListView {
                id:view
                anchors.fill:parent
                model : listModel
                delegate: Rectangle {
                     width:parent.width
                     height:80
                     Text {
                     anchors.center:parent
                     text: title
                     }
                   
                }
            }
            

            }
            @

            javascript side : parser.js

            @
            function load() {

            listModel.clear();
            var xhr = new XMLHttpRequest();
            

            xhr.open("GET","http://data.json",true);
            xhr.onreadystatechange = function()
            {
            if ( xhr.readyState == xhr.DONE)
            {
            if ( xhr.status == 200)
            {
            var jsonObject = eval('(' + xhr.responseText + ')');
            loaded(jsonObject)
            }
            }
            }
            xhr.send();
            }

            function loaded(jsonObject)
            {
            for ( var index in jsonObject.flux.all )
            {
            listModel.append({
            "title" : jsonObject.flux.all[index].data["title"],
            "icon" : jsonObject.flux.all[index].data["icon"]});
            }

            // get directly the json object. Should work but not tested
            //listModel.append({jsonObject.flux.all});

            }
            @

            Nothing in Biology Makes Sense Except in the Light of Evolution

            1 Reply Last reply
            0
            • C Offline
              C Offline
              changsheng230
              wrote on 24 Jul 2011, 12:27 last edited by
              #6

              Thanks for your sample codes, but it seems not work still, such as "anchors.center" should "anchors.centerIn:"
              And how to put the JSON data into a place, which http address is able to access to.

              [quote author="dridk" date="1308668221"]Hello here is my point of view! I tested everything! XMLModel, javascript XML parser, C++ side parser etc...

              Do Not use C++ Json parser : Because exchange data between c++ and QML is slow

              Make a Javascript parser ! It's more easy with eval ! Here is an example :

              Json data
              @
              {"index":["all"],"flux":{"all":[{"data":{"title":"boris","icon":"icon.png"}]}}
              @

              QML side : main.qml
              @
              import QtQuick 1.0
              import "parser.js" as JS

              Item {
              id:root
              width: 360
              height: 640

              Component.onCompleted: JS.load()
              
              
              ListModel {  id:listModel }
              
              ListView {
                  id:view
                  anchors.fill:parent
                  model : listModel
                  delegate: Rectangle {
                       width:parent.width
                       height:80
                       Text {
                       anchors.center:parent
                       text: title
                       }
                     
                  }
              }
              

              }
              @

              javascript side : parser.js

              @
              function load() {

              listModel.clear();
              var xhr = new XMLHttpRequest();
              

              xhr.open("GET","http://data.json",true);
              xhr.onreadystatechange = function()
              {
              if ( xhr.readyState == xhr.DONE)
              {
              if ( xhr.status == 200)
              {
              var jsonObject = eval('(' + xhr.responseText + ')');
              loaded(jsonObject)
              }
              }
              }
              xhr.send();
              }

              function loaded(jsonObject)
              {
              for ( var index in jsonObject.flux.all )
              {
              listModel.append({
              "title" : jsonObject.flux.all[index].data["title"],
              "icon" : jsonObject.flux.all[index].data["icon"]});
              }

              // get directly the json object. Should work but not tested
              //listModel.append({jsonObject.flux.all});

              }
              @[/quote]

              Chang Sheng
              常升

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fcrochik
                wrote on 24 Apr 2012, 18:34 last edited by
                #7

                Instead of:

                @var jsonObject = eval(’(’ + xhr.responseText + ‘)’); @

                one can simply use in QML (without any imports or includes):

                @ var jsonObject = JSON.parse(xhr.responseText); @

                It is safer and may actually be faster (if it is not already, with Qt5 it will be).

                Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wilson_chan
                  wrote on 13 Jul 2012, 04:00 last edited by
                  #8

                  [quote author="dridk" date="1308668221"]Hello here is my point of view! I tested everything! XMLModel, javascript XML parser, C++ side parser etc...

                  Do Not use C++ Json parser : Because exchange data between c++ and QML is slow

                  Make a Javascript parser ! It's more easy with eval ! Here is an example :

                  Json data
                  @
                  {"index":["all"],"flux":{"all":[{"data":{"title":"boris","icon":"icon.png"}]}}
                  @

                  QML side : main.qml
                  @
                  import QtQuick 1.0
                  import "parser.js" as JS

                  Item {
                  id:root
                  width: 360
                  height: 640

                  Component.onCompleted: JS.load()
                  
                  
                  ListModel {  id:listModel }
                  
                  ListView {
                      id:view
                      anchors.fill:parent
                      model : listModel
                      delegate: Rectangle {
                           width:parent.width
                           height:80
                           Text {
                           anchors.center:parent
                           text: title
                           }
                         
                      }
                  }
                  

                  }
                  @

                  javascript side : parser.js

                  @
                  function load() {

                  listModel.clear();
                  var xhr = new XMLHttpRequest();
                  

                  xhr.open("GET","http://data.json",true);
                  xhr.onreadystatechange = function()
                  {
                  if ( xhr.readyState == xhr.DONE)
                  {
                  if ( xhr.status == 200)
                  {
                  var jsonObject = eval('(' + xhr.responseText + ')');
                  loaded(jsonObject)
                  }
                  }
                  }
                  xhr.send();
                  }

                  function loaded(jsonObject)
                  {
                  for ( var index in jsonObject.flux.all )
                  {
                  listModel.append({
                  "title" : jsonObject.flux.all[index].data["title"],
                  "icon" : jsonObject.flux.all[index].data["icon"]});
                  }

                  // get directly the json object. Should work but not tested
                  //listModel.append({jsonObject.flux.all});

                  }
                  @[/quote]

                  Hi,
                  My name is wilson. I have test out using this method to parsing Json with Xml.
                  But when I click on the page, it shown a blank page.
                  May I know why is this happen??

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved