json - 重新格式化 JSON 数据以适应树状存储

标签 json sencha-touch sencha-touch-2 sencha-touch-2.1

我想使用服务器提供的数据制作购物类别的嵌套列表,如下所示:

{
    "status":{"statusCode":15001},
    "data":[
        {"itemId":1, "name":"Men", "subCategories":[
            {"itemId":2, "name":"Clothes", "subCategories":[
                {"itemId":3, "name":"Formals", "leaf":true,"subCategories":[]},
                {"itemId":4, "name":"Casual", "leaf":true,"subCategories":[]},
                {"itemId":5, "name":"Sports", "leaf":true,"subCategories":[]}
            ]},
            {"itemId":6, "name":"Accessories", "subCategories":[
                {"itemId":7, "name":"Formals", "leaf":true,"subCategories":[]},
                {"itemId":8, "name":"Casual", "leaf":true,"subCategories":[]},
                {"itemId":9, "name":"Sports", "leaf":true,"subCategories":[]}
            ]}
        ]},
        {"itemId":10, "name":"Women", "subCategories":[
            {"itemId":11, "name":"Clothes", "subCategories":[
                {"itemId":12, "name":"Formals", "leaf":true,"subCategories":[]},
                {"itemId":13, "name":"Casual", "leaf":true,"subCategories":[]},
                {"itemId":14, "name":"Ethnic", "leaf":true,"subCategories":[]}
            ]},
            {"itemId":15, "name":"Shoes", "subCategories":[
                {"itemId":16, "name":"Hells", "leaf":true,"subCategories":[]},
                {"itemId":17, "name":"Wedges", "leaf":true,"subCategories":[]},
                {"itemId":18, "name":"Sports", "leaf":true,"subCategories":[]}
            ]}
        ]}
    ]
}

由于树结构被包裹在 data 中element 和 children 被包裹在 subCategories 中与 data 不同的标签所以我想对这些数据进行预处理,以便 Nested List 可以使用它直接通过做出这样的回应:
{
    "categories":[
        {"itemId":1, "name":"Men", "categories":[
            {"itemId":2, "name":"Clothes", "categories":[
                {"itemId":3, "name":"Formals", "leaf":true,"categories":[]},
                {"itemId":4, "name":"Casual", "leaf":true,"categories":[]},
                {"itemId":5, "name":"Sports", "leaf":true,"categories":[]}
            ]},
            {"itemId":6, "name":"Accessories", "categories":[
                {"itemId":7, "name":"Formals", "leaf":true,"categories":[]},
                {"itemId":8, "name":"Casual", "leaf":true,"categories":[]},
                {"itemId":9, "name":"Sports", "leaf":true,"categories":[]}
            ]}
        ]},
        {"itemId":10, "name":"Women", "categories":[
            {"itemId":11, "name":"Clothes", "categories":[
                {"itemId":12, "name":"Formals", "leaf":true,"categories":[]},
                {"itemId":13, "name":"Casual", "leaf":true,"categories":[]},
                {"itemId":14, "name":"Ethnic", "leaf":true,"categories":[]}
            ]},
            {"itemId":15, "name":"Shoes", "categories":[
                {"itemId":16, "name":"Hells", "leaf":true,"categories":[]},
                {"itemId":17, "name":"Wedges", "leaf":true,"categories":[]},
                {"itemId":18, "name":"Sports", "leaf":true,"categories":[]}
            ]}
        ]}
    ]
}

为此,我覆盖了 getResponseData读取器,但此方法永远不会被调用,也不会在存储中加载任何记录。我究竟做错了什么?

这是我的商店:
Ext.define('MyTabApp.store.CategoriesStore',{
    extend:'Ext.data.TreeStore',
    config:{
        model   : 'MyTabApp.model.Category',
        autoLoad: false,
        storeId : 'categoriesStore',
        proxy: {
            type: 'ajax',
            url: Properties.CONFIG_SERVICE_BASE_URL+'topnav/getall',
            reader: {
                type: 'json',
                getResponseData: function(response) {
                    console.log("in getResponseData"); // Never logged in console
                    var rText = response.responseText;
                    var processed = Helper.replaceAll("data","categories",rText);
                    processed = Helper.replaceAll("subCategories","categories",processed);
                    var respObj = Ext.JSON.decode(processed);
                    return respObj.categories;
                }
            }
        },
        listeners:{
            load: function( me, records, successful, operation, eOpts ){ 
                console.log("categories tree loaded");
                console.log(records); // This prints blank array
            }
        }
    }
});

这是模型:
Ext.define('MyTabApp.model.Category', {
    extend : 'Ext.data.Model',
    config : {
        idProperty  : 'itemId',
        fields      : [ 
           {name : 'itemId',type : 'int'}, 
           {name : 'name',type : 'string'}
        ]
    }
});

这是 list :
Ext.define('MyTabApp.view.CategoriesList', {
    extend: 'Ext.dataview.NestedList',
    alias : 'widget.categorieslist',
    config: {
        height              : '100%',
        title               : 'Categories',
        displayField        : 'name',
        useTitleAsBackText  : true,
        style               : 'background-color:#999 !important; font-size:75%',
        styleHtmlContent    : true,
        listConfig: {
            itemHeight: 47,
            itemTpl : '<div class="nestedlist-item"><div style="position:absolute; left:10px; top:10px; color:#222; font-size:130%">{name}</div></div>',
            height : "100%"
        }
    },
    initialize : function() {
        this.callParent();
        var me = this;

        var catStore = Ext.create('MyTabApp.store.CategoriesStore');
        catStore.load();
        me.setStore(catStore);
    }
});

如果接收的数据不是我们想要的格式并且我们无法控制服务,那么处理和格式化接收到的数据的最佳做法是什么?

最佳答案

我认为您不能将 getResponseData 附加到阅读器的配置中。过去我使用了以下方法。

  • 创建 Ext.data.reader.Json 的覆盖,如下所示。但缺点是这将使用 json 读取器为您的所有代理调用。然后在应用程序的 requires 部分添加 YourApp.override.data.reader.Json 。
    Ext.define('YourApp.override.data.reader.Json', { override: 'Ext.data.reader.Json', 'getResponseData': function(response) { // your implementation here }
  • 或者,您可以创建一个 Ext.Ajax.request 并在成功处理程序中以您想要的方式解析数据,然后在您的商店中设置数据。也可以在store的“refresh”事件或“beforeload”事件中写这段代码,返回false取消 Action
  • 关于json - 重新格式化 JSON 数据以适应树状存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16852095/

    相关文章:

    javascript - 多个 xmlhttp 请求 - JSON get 被覆盖

    javascript - Sencha Touch 2 : tapping Ext. Msg.show 没有按钮

    javascript - Sencha Touch 构建 - 排除文件

    javascript - 使用 HTML 文件作为 Sencha 的模板?

    javascript - 在推送 View 上隐藏按钮并在返回 ListView 时显示它

    html - 列表项 Tpl 问题 : List that has not been rendered with two arrays as two columns

    c# - 如何在 Angular 2 中序列化/反序列化 .NET 类型的 JSON

    javascript - 隐藏多个json d3组

    jquery - Ajax 和 Places API 获取请求

    android - 链接点击打开 Android 浏览器并退出应用程序