pm13
pm13

Reputation: 745

Cannot access Ajax JSON data objects

I have a JSON response returning from an Ajax call but cannot seem to access any part of the JSON at all.

The JSON format is: [{"id":"1","description":"Employee","coverage":"Center","covered":"X"}]

I have tried the following and nothing works:

success: function(result, request){
    jsonData = Ext.util.JSON.decode(result.responseText);
    var id = jsonData.id;
    alert(id);
 }

 * returns as undefined

success: function(result,request){
    jsonData = result.responseText  ##shows the Json perfectly
    alert(jsonData.length) ### displays as number of chars, not how many objects in json string
}

Upvotes: 2

Views: 4510

Answers (2)

Thevs
Thevs

Reputation: 3253

You may use "evil" eval for this:

var jsonData;
eval('jsonData =' + result.responseText);
alert(jsonData[0].id);

Upvotes: 0

Saket Patel
Saket Patel

Reputation: 6683

Ext.util.JSON.decode is ExtJS3 method and Ext.JSON.decode is in ExtJS4, as you have not indicated which version of ExtJS you are using so failsafe way will be to use Ext.decode which is available in both ExtJS3 and ExtJS4

success: function(result, request){
    jsonData = Ext.decode(result.responseText);
    console.log(jsonData);
}

Upvotes: 5

Related Questions