Cannot access Ajax JSON data objects

你。 提交于 2019-12-24 03:28:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!