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?
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