我有一个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字符串格式?
每个建议都将不胜感激。
答案 0 :(得分: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
注意:您必须在字典Keys上调用.ToString(),因为JavaScript Serializer似乎在Dictionarys上使用不是字符串的键失败...
答案 1 :(得分: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));
这使用属于LINQ的ToDictionary()
扩展方法。
输出:
{"1":{"1":"Lzwuoge","2":"0lzpas"},"2":{"1":"Mqn3ul5k","2":"Kefzu"}}
答案 2 :(得分: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)
}