开发者

Parse JSON to Extjs Store

I use ExtJS4 to request data in format id-label ('id'-'rdfs:label' in my case).

I have complex structure of JSON response:

[
{
"@" : "msg:M_8CE1C07B1CFC469",
"a" : "msg:Message",
"msg:in-reply-to" : "msg:M4e32b0b7e37ae",
"msg:sender" : "pacahon",
"msg:reciever" : "Ennoia:DocumentTypesStore.listTemplates",
"msg:result" : [
    {
     "@" : "user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8",
     "dc:creator" : "auth:user1",
     "dc:identifier" : "881fc0a8-2912-4964-86c6-adad4a713eb8",
     "rdfs:label" : "undefined|ru",
     "rdfs:subClassOf" : ["user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8_v_1_f_0","user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8_v_1_f_1","user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8_v_1_f_2","user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8_v_1_f_3","docs:Document"]
    },
    {
     "@" : "user_onto:ef54ecef-8e19-41ea-b1cc-2bc4ebb40877",
     "dc:creator" : "auth:user1",
     "dc:identifier" : "ef54ecef-8e19-41ea-b1cc-2bc4ebb40877",
     "rdfs:label" : "type1|ru",
     "rdfs:subClassOf" : ["user_onto:ef54ecef-8e19-41ea-b1cc-2bc4ebb40877_v_1_f_0","user_onto:ef54ecef-8e19-41ea-b1cc-2bc4ebb40877_v_1_f_1","user_onto:ef54ecef-8e19-41ea-b1cc-2bc4ebb40877_v_1_f_2","user_onto:ef54ecef-8e19-41ea-b1cc-2bc4ebb40877_v_1_f_3","docs:Document"]
    }],
"msg:status" : "ok",
"msg:reason" : "запрос выполнен: авторизованны все найденные субьекты :     2"
}
] 

I want to read this json in my store and get something like this:

[{"@" : "user_onto:881fc0a8-2912-4964-86c6-adad4a713eb8"
  "rdfs:label" : "undefined|ru"},
 {"@" : "user_onto:ef54ecef-开发者_开发百科8e19-41ea-b1cc-2bc4ebb40877"
  "rdfs:label" : "type1|ru"}]

Notice, I don't want to change this JSON response (remove or add braces).


I used API Doc to solve my problem (Ext.data.reader.Reader):

        Ext.define("Response", {
                extend: 'Ext.data.Model',
                fields: [
                    {name:'id', type:'string', mapping:'@'}
                ],

                hasMany: {model: 'Message', name: 'messages', associationKey:'msg:result'},
            });

        Ext.define("Message", {
                extend: 'Ext.data.Model',
                fields: [{name:'id', type:'string', mapping:'@'}, {name:'title', type:'string', mapping:'rdfs:label'}],
                belongsTo: 'Response'
          });

Then I add Response as model for my JsonStore:

        proxy: {
                type: 'ajax',
                url: './controller.php?class=DocumentTypesStore&action=get&method=listTemplates',           
                method: "POST",
                actionMethods: {read: 'POST'},          
                reader: {
                    type: 'json',
                    root: [],
                            idProperty: '@'
                }
            }

And I can read values now:

    jsonstore.on('load', function(){
        var response = jsonstore.first();
        console.log(response.get('id'));
        response.messages().each(function(message) {
            console.log(message.get('id'));
        });         
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜