生成分类树结构JSON数据? [英] generate category tree structure json data?

查看:127
本文介绍了生成分类树结构JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分类表:

ID =>整型,主键

ID => int, primary key

类别名称=> VARCHAR

CategoryName => varchar

ParentCategoryID => INT,可为空

ParentCategoryID => int , nullable

生成以下JSON数据格式为:

[{
    "id":1,
    "name":"Root",
    "Edit":"<a href='Edit/id'>edit.png</a>",
    "Delete":"<a href='Delete/id'>delete.png</a>",
    "children":[{
        "id":2,
        "name":"Horoscope",
        "Edit":"<a href='Edit/id'>edit.png</a>",
        "Delete":"<a href='Delete/id'>delete.png</a>",
        "children":[{
            "id":21,
            "name":"Daily",
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>",
            "children":[{
                "id":211,
                "name":"Aries",             
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":212,
                "name":"Taurus",                
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            }]
        },{
            "id":22,
            "name":"Weekly",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>",
            "children":[{
                "id":221,
                "name":"Gemini",                
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":222,
                "name":"Aries",             
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":223,
                "name":"Taurus",            
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            }]
        }]
    },{
        "id":3,
        "name":"News",      
        "Edit":"<a href='Edit/id'>edit.png</a>",
        "Delete":"<a href='Delete/id'>delete.png</a>",
        "children":[{
            "id":31,
            "name":"Sports",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        },{
            "id":32,
            "name":"Interantional",         
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        },{
            "id":33,
            "name":"Entertaintment",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        }]
    }]
}]

我的Jquery的TreeGrid插件,需要上面的JSON格式。怎样才能上述JSON数据从数据库类别表,并在控制器动作返回JSON数据。我有一个模型使用实体框架。

I have Jquery TreeGrid Plugins which needs above json format. How Can I generate above json data from category table of database and return json data in controller action. I have use entity framework for model.

推荐答案

我不会混合数据和UI指令下手!它杂波与冗余重复数据的JSON消息。我认为这是由客户端来决定把数据以及如何显示它们,否则你应该单独发送这些指令(例如作为消息的第一部分)。

I would not mix data and UI instructions to start with! It clutters the JSON message with redundant repetitive data. I think it's up to the client to decide where to put the data and how to display them, or otherwise you should send these instructions separately (e.g. as first part of the message).

这是说,要完成这件事的最简单方法是让您的类定义是这样的:

That said,the easiest way to get this done is having your class definition like this:

class Category
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public int? ParentCategoryID { get; set; }
    [ForeignKey("ParentCategoryID")]
    public virtual ICollection<Category> SubCategories { get; set; }
}

当你查询类别 ParentCategoryID == NULL ,并启用延迟加载,并序列化成JSON你 LL看到,所有级别都包含,因为查询发出来获得它的孩子们每个类别。

When you query Categories with ParentCategoryID == null, with lazy loading enabled, and serialize into JSON you'll see that all levels are included because for each Category a query is emitted to get its children.

注意类别不具有 ParentCategory 属性,因为这可能会导致JSON序列化,因为循环引用失败

Note that Category does not have a ParentCategory property because that may cause JSON serialization to fail because of circular references.

有关序列化,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\"相对=nofollow> 的JavaScriptSerializer ,如果​​你不是在MVC API控制器。

For serialization you could use JavaScriptSerializer if you're not in MVC API controllers.

BTW。该的的办法做到这一点会离开你的域模型运输无知,并使用 CategoryDto 对象的结构。

BTW. The best way to do this would be to leave your domain model transport-ignorant and use a structure of CategoryDto objects.

这篇关于生成分类树结构JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆