0

I am new to Blackberry development and I'm stuck on JSON parsing. I've learnt how to call a web service in QML but I don't know how to parse the result I obtain which is JSON. Anyone can help me?

Thank you in advance.

My actual code:

Button {
    id: newButton
    horizontalAlignment: HorizontalAlignment.Center
    verticalAlignment: VerticalAlignment.Center
    topMargin: ui.du(3)
    text: "Recharge"
    appearance: ControlAppearance.Primary

    onClicked: {
        getData()
    }
}

function getData() {
    var xmlhttp = new XMLHttpRequest();
    var url = "http://sb2.in/AppServices.asmx/getoperator";

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            myFunction(xmlhttp.responseText);
            //console.log(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

function myFunction(response) {
    var arr = JSON.parse(response);  
    console.log(response);     // (1) 
}

Here is the JSON response I get, printed in (1):

[{"CircleID":1,"CircleName":"Andhra Pradesh"},{"CircleID":4,"CircleName":"Assam"},{"CircleID":5,"CircleName":"Bihar"},{"CircleID":6,"CircleName":"Chennai"},{"CircleID":7,"CircleName":"Delhi"},{"CircleID":8,"CircleName":"Gujarat"},{"CircleID":9,"CircleName":"Himachal Pradesh"},{"CircleID":10,"CircleName":"Haryana"}]

How can I correctly parse this JSON response?

kartheeki j
  • 2,206
  • 5
  • 27
  • 51
  • Marked as too broad. Please read [how to ask](http://stackoverflow.com/help/how-to-ask) a question on SO. – BaCaRoZzo Jul 20 '15 at 20:29
  • @BaCaRoZzo please tell me how to parse a json in qml! – kartheeki j Jul 21 '15 at 04:45
  • It depends on the returned response. However you can access fields as explained [here](http://supportforums.blackberry.com/t5/Native-Development/Parsing-a-JSON-in-QML/td-p/2648491). If a field is comma-separated use `split(',')` on it. – BaCaRoZzo Jul 21 '15 at 05:56
  • @BaCaRoZzo thank you for response. i added my json response. how can i parse json array and json object (different forms of json). if you have any links please comment me. I will learn .....thank you – kartheeki j Jul 21 '15 at 06:06
  • I think [this](http://stackoverflow.com/a/9991872/2538363) is the answer you are looking for. I just searched the answer on SO... – BaCaRoZzo Jul 21 '15 at 07:15
  • @BaCaRoZzo then if we know javascript we can do the application with the help of Qml only. i have a doubt that with the help of qml only we can do the BB app or need to use c++ too? – kartheeki j Jul 21 '15 at 07:18
  • It really depends on what you have to do. For parsing JSON you can use QML only, as you can see. Anyhow, adding a C++ class is not that difficult. – BaCaRoZzo Jul 21 '15 at 07:24
  • @BaCaRoZzo thank you for useful info to me. For my app i need to work with web services ( for login,register, payment). for payment i will use web view. so we can develop an app with the help of qml only! – kartheeki j Jul 21 '15 at 07:32
  • Probably, possibly. As said, adding C++ features is quite easy and also enables you to access all the Qt features, even those not available in QML. – BaCaRoZzo Jul 21 '15 at 07:36
  • @BaCaRoZzo Ok,, :( but i am not familar with c++ and qml. But qml is looking simple than c++. Thats'y i am asking so many times :( – kartheeki j Jul 21 '15 at 07:41

2 Answers2

1

Use DataSource.

At the top of QML add import bb.data 1.0

attachedObjects: [
    DataSource {
      id: dataSource
      source: "http://mywebsite.com"
      type: DataSourceType.Json

      onDataLoaded: {
          //data is already parsed!
          console.log("Data: " + JSON.stringify(data));
      }
   }
]
onCreationCompleted: {
    dataSource.load(); 
}

Add this in your .pro file LIBS += -lbbdata

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
1

Thank you @BaCaRoZzo.

web service & json parsing:

 function getCircle() {
                var xmlhttp = new XMLHttpRequest();
                var url = "http://sb2.in/AppServices.asmx/getRecharge";

                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        circleFunction(xmlhttp.responseText);

                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }
            function circleFunction(circleresponse){
                var circle = JSON.parse(circleresponse);  
                // console.log(circleresponse);// json 
                for (var k = 0; k< circle.length; k++) {
                    var circles = circle[k];
                    console.log("CircleName"+circles.CircleID);
                    console.log("CircleName"+circles.CircleName);

                }
            }

@BaCaRoZzo link: Javascript how to parse JSON array

Community
  • 1
  • 1
kartheeki j
  • 2,206
  • 5
  • 27
  • 51