问题
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
}
回答1:
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);
}
回答2:
You may use "evil" eval for this:
var jsonData;
eval('jsonData =' + result.responseText);
alert(jsonData[0].id);
来源:https://stackoverflow.com/questions/10143516/cannot-access-ajax-json-data-objects