我可以让JavaScriptSerializer分层次地序列化LINQ结果吗?

时间:2011-05-25 14:35:26

标签: .net linq json javascriptserializer

我这样做:

var data = from a in attributes
           from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
           .DefaultIfEmpty(new DocClassAttributeFieldItem())
                select new
                {
                    Id = a.Id,
                    LabelText = a.LabelText,
                    Items = i
                };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

结果如下:

[{
    "Id": 1,
    "LabelText": "Unit On-Line Status:",
    "Items": {
        "Id": 1,
        "DocClassAttributeFieldId": 1,
        "LabelText": "Online",
        "ValueText": "Online",
        "Ordering": 1
    }
},
{
    "Id": 1,
    "LabelText": "Unit On-Line Status:",
    "Items": {
        "Id": 2,
        "DocClassAttributeFieldId": 1,
        "LabelText": "Offline",
        "ValueText": "Offline",
        "Ordering": 2
    },
}]

我想得到这样的结果:

[{    
    "Id": 1,    
    "LabelText": "Unit On-Line Status:",    
    "Items": [{    
        "Id": 1,    
        "DocClassAttributeFieldId": 1,    
        "LabelText": "Online",    
        "ValueText": "Online",    
        "Ordering": 1    
    },{    
        "Id": 2,    
        "DocClassAttributeFieldId": 1,    
        "LabelText": "Offline",    
        "ValueText": "Offline",    
        "Ordering": 2    
    }]    
}]

这可以通过JavaScriptSerializer轻松完成,还是可以重新编写LINQ语句来生成它?

更新:感谢这些帖子......

http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

我不打算使用JavaScriptSerializer,ASP.Net为我做了一切:

[WebMethod]
public static object GetDocClass(int docClassId)
{
    var data = from a in attributes                       
        select new
        {
            Id = a.Id,
            LabelText = a.LabelText,
            Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)                    
        };          
    return data;
}

2 个答案:

答案 0 :(得分:2)

您可以这样做,但您需要使用组来填充项目。

var data = 
       from a in attributes
       from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
       group a by a.Id, a.LabelText into myGroup
       .DefaultIfEmpty(new DocClassAttributeFieldItem())
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = myGroup.ToList()
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

这是从臀部开始拍摄的,所以如果它不适合你,请告诉我。

答案 1 :(得分:1)

可能有更好的方法,但Nix的回答帮助我想出了这个:

var data = from a in attributes                       
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)                    
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);