user969275
user969275

Reputation: 284

JSON parsing using JavaScript for PhoneGap Android application

In my application I want to hit an URL and I use to get a return data in JSON format as follows:

{
    "Status": {
        "itemlist": [{
            "image": "http://XXXXXXXXXXXXXXXXXX.com/images/original/1316145577.jpg",
            "id": "1"
            Name: "xxx"
        }, {
            "image": "http://XXXXXXXXXXXXXXXXXX.com/images/original/1316145577.jpg",
            "id": "2"
            Name: "xxx"
        }]
    }
}

I want to do JSON parsing and display the return value in listview using JavaScript. Please help me. I have parsed using the following code:

function appReady() {
    alert("verified");
    var API = "http://XXXXXXXXXXXXXXXXXXXXXXXXX";
    check(API);
}

function check(API) {
    alert("entered");
    var http_request = new XMLHttpRequest();
    alert(http_request);
    http_request.open("GET", API, false);
    http_request.send(null);
    var my_JSON_object = http_request.responseText;
    alert(my_JSON_object);
    var data = JSON.parse(my_JSON_object);
    var Itemlist = [];
    for (var i = 0; i < Status.length; i++) {
        Itemlist[i] = data.Status.Itemlist[0].id;
        alert(id);
        Itemlist[i] = data.Status.Itemlist[0].Name;
        alert(Name);
        Itemlist[i] = data.Status.Itemlist[0].image;
        alert(image);
    }
    alert("id");
}
document.addEventListener("deviceready", appReady, false);

I want show progress spinner instead of alert thrown in my code. I also want to make a list view with return data. Please help me.

Upvotes: 1

Views: 4353

Answers (1)

xkeshav
xkeshav

Reputation: 54022

first there are some syntax error in given JSON string.

you can try this way

var itemList = json.Status.itemlist;
for(var i=0;i<itemList.length;i++)
{
    alert('Image:'+itemList[i].image
     +'\nID:'+itemList[i].id
        +'\nName:'+itemList[i].Name);
}

see the

DEMO

Upvotes: 1

Related Questions