Sencha Touch 2:将非标准JSON加载到存储中
Sencha Touch web应用程序从REST web服务提交的JSON加载存储。对于其他技术要求,JSON不是标准(但仍然有效),并遵循以下模式:Sencha Touch 2:将非标准JSON加载到存储中,json,extjs,sencha-touch,sencha-touch-2,Json,Extjs,Sencha Touch,Sencha Touch 2,Sencha Touch web应用程序从REST web服务提交的JSON加载存储。对于其他技术要求,JSON不是标准(但仍然有效),并遵循以下模式: { "101": { "id": "101", "title": "title 101", "description": "description 101", "order": "20", "thumb": { "id": "1010
{
"101": {
"id": "101",
"title": "title 101",
"description": "description 101",
"order": "20",
"thumb": {
"id": "10101",
"uri": "http://linktothumb.jpg"
},
"parent": "0",
"cType": "5"
},
"102": {
"id": "102",
"title": "title 102",
"description": "description 102",
"order": "59",
"thumb": {
"id": "10102",
"uri": "http://linktothumb.jpg"
},
"parent": "101",
"cType": "5"
}
}
不幸的是,当我执行myStore.load()时,ST无法解析JSON,并且数据没有添加到存储中。但是,如果JSON响应遵循以下模式,它就会工作:
[
{
"id": "101",
"title": "title 101",
"description": "description 101"
"thumb": {
"id": "10101",
"uri": "http://linktothumb.jpg"
},
"parent": "0"
},
{
"id": "102",
"title": "title 102",
"description": "description 102"
"thumb": {
"id": "10102",
"uri": "http://linktothumb.jpg"
},
"parent": "101"
}
]
我无法更改JSON的格式,因此我需要找到一种方法使其与此响应一起工作。是否可以向ST的JSON读取器添加参数,或者手动解析JSON响应?如果是,如何进行
这是我的店铺的代理:
proxy: {
type:'rest',
useDefaultXhrHeader : false,
headers: {
'Accept': 'application/json'
},
reader: {
type:'json'
}
}
您可以创建自定义读卡器:
Ext.define("App.data.reader.MyReader", {
extend: 'Ext.data.reader.Json',
getResponseData: function(response) {
var data, error;
try {
data = this.parseResponse(Ext.decode(response.responseText));
return this.readRecords(data)
} catch (ex) {
error = new Ext.data.ResultSet({
total : 0,
count : 0,
records: [],
success: false,
message: ex.message
});
this.fireEvent('exception', this, response, error);
Ext.Logger.warn('Unable to parse the JSON returned by the server');
return error;
}
},
parseResponse: function(data) {
return data;
}
});
通过自定义parseResponse,您可以按照自己的方式处理数据。感谢您提供此解决方案。parseResponse函数应该返回哪种对象类型和结构来填充存储?看看错误案例,您必须返回这样一个模板,其中记录是要返回的单个模型的数组。