var store = "{ id: '1', text: '测试1-1' }, { id: '2', text: '测试1-2' }, { id: '3', text: '测试1-3'}"; var jsonStore = Ext.util.JSON.decode(store); 为什么jsonStore返回的只剩最后一个"测试1-3"的,前面的都不见了 Ext.util.JSON.decode()是將字串轉變為JSON object型態,
以上述的例子 var store = "{ id: '1', text: '测试1-1' }, { id: '2', text: '测试1-2' }, { id: '3', text: '测试1-3'}"; 來說 其實並不是一個JSON型態的字串, 我在此作一個假設,
假設您的字串為 var store = "{ rows : [{ id: '1', text: '测试1-1' }, { id: '2', text: '测试1-2' }, { id: '3', text: '测试1-3'}] }";
如此一來,使用var jsonStore = Ext.util.JSON.decode(store)便會得到json object jsonStore.rows[0].id -->'1' jsonStore.rows[1].id -->'2' jsonStore.rows[2].id -->'3' jsonStore.rows[0].text --> '测试1-1'
上面的这些代码是我网上找来的,下面是我自己的实践可以成功得到数据
//取后台数据
function getManager(deptId){//传一个部门id
Ext.Ajax.request({
url: '/pss/deptemps_getManagerInfo.do?departmantId='+deptId,
success: function(res){
//Ext.Msg.alert("提示消息",res.responseText);
var jsonResult = Ext.util.JSON.decode(res.responseText);
alert(jsonResult.manager.name);//我返回的数据只有一个,这里也不能用manager[0].name
Ext.Msg.alert("提示",jsonResult.manager.name);
},
failure: function(){
Ext.Msg.alert("发送失败","失败");
}
});
}
后台的响应参数为:
{"manager":{"name":"sammor","userId":1002}}
以上供大家互相学习参考
本文探讨了使用 Ext.util.JSON.decode 方法解析不规范 JSON 字符串的问题,并提供了一个正确的 JSON 格式示例及如何正确获取数据的方法。
1000

被折叠的 条评论
为什么被折叠?



