1

我有一个Generic List对象,它把下表作为它自己的值。

CountryID | StateID | StateName
--------------------------------
    1     |  1      |  Alabama    
    1     |  2      |  California
    1     |  3      |  Florida
    1     |  4      |  Hawaii   
    2     |  5      |  London
    2     |  6      |  Oxford

我想根据该 List 对象创建 JSON 字符串。我想得到的 JSON 格式如下所示。

{           
    1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
    2: { '5': 'London', '6': 'Oxford' }
};

我使用下面的类来生成 JSON 对象。

public static class JSONHelper
{
    public static string ToJSON(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

    public static string ToJSON(this object obj, int recursionDepth)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RecursionLimit = recursionDepth;            
        return serializer.Serialize(obj);
    }
}

但是,我完成调用ToJSON方法后得到的实际输出如下所示。

{
[

{"CountryID":1,"StateID":1,"StateName":"Alabama"},
{"CountryID":1,"StateID":2,"StateName":"California"},
{"CountryID":1,"StateID":3,"StateName":"Florida"},
{"CountryID":1,"StateID":4,"StateName":"Hawaii"},
{"CountryID":2,"StateID":5,"StateName":"London"},
{"CountryID":2,"StateID":6,"StateName":"Oxford"}

]
}

那么,任何人都可以给我建议如何制作我想要的 JSON 字符串格式吗?
每一个建议都将不胜感激。

4

3 回答 3

3

似乎一种简单的方法是使用 LINQ 选择字典,然后将字典对象发送到 JavascriptSerializer ...

假定我们有:

var list = new[]
               {
                   new {CountryID = 1, StateID = 1, StateName = "Alabama"},
                   new {CountryID = 1, StateID = 2, StateName = "California"},
                   new {CountryID = 1, StateID = 3, StateName = "Florida"},
                   new {CountryID = 1, StateID = 4, StateName = "Hawaii"},
                   new {CountryID = 2, StateID = 5, StateName = "London"},
                   new {CountryID = 2, StateID = 6, StateName = "Oxford"}
               };

然后我们可以在其上递归调用 .ToDictionary 以获得以下信息:

var d = list
    .GroupBy(x=>x.CountryID)
    .ToDictionary(g=> g.Key.ToString(), 
        g => g.ToDictionary(x => x.StateID.ToString(),x => x.StateName));

var serializer = new JavaScriptSerializer();
return serializer.Serialize(d);

它返回您请求的 JSON

注意:您必须在字典键上调用 .ToString() ,因为 JavaScriptSerializer 似乎在带有非字符串键的字典上失败...

于 2012-06-21T05:43:44.343 回答
3

您需要序列化字典而不是列表来获取 JSON。鉴于这些类型定义:

public class Country
{
    public Country()
    {
        States = new List<State>();
    }
    public int         CountryID  { get; set; }
    public List<State> States     { get; set; }
}

public class State
{
    public int    StateID    { get; set; }
    public string StateName  { get; set; }
}

而这个变量:

    var clist = new List<Country>();

我可以使用以下代码序列化为您想要的格式:

    var d = clist.ToDictionary
        (k => k.CountryID.ToString(),
         e => e.States.ToDictionary(k2 => k2.StateID.ToString(),
                                    e2 => e2.StateName));

    Console.WriteLine("{0}",JSONHelper.ToJSON(d));

这使用作为ToDictionary()LINQ 一部分的扩展方法。

输出:

{"1":{"1":"Lzwuoge","2":"0lzpas"},"2":{"1":"Mqn3ul5k","2":"Kefzu"}}
于 2012-06-21T05:47:06.383 回答
1

将其转换为您指定的格式的最简单方法是将您的自定义类转换为嵌套字典:

Dictionary<Int32,Dictionary<String, String>>

然后序列化该输出。

Dictionary<Int32,Dictionary<String, String>> countryDict= new Dictionary<Int32,Dictionary<String, String>>()
foreach(var item in myGenericList){
    Dictionary<Int32,Dictionary<String, String> stateDict = 
    countryDict[item.CountryID] ?? new Dictionary<Int32,Dictionary<String, String>>();
    stateDict.Add(item.StateID.ToString(),item.StateName) 
}
于 2012-06-21T05:15:54.490 回答