C# 使用自定义格式序列化JSON字符串
我有一个通用列表对象,它将下表作为自己的值保存C# 使用自定义格式序列化JSON字符串,c#,json,serialization,C#,Json,Serialization,我有一个通用列表对象,它将下表作为自己的值保存 CountryID | StateID | StateName -------------------------------- 1 | 1 | Alabama 1 | 2 | California 1 | 3 | Florida 1 | 4 | Hawaii 2 | 5 | Londo
CountryID | StateID | StateName
--------------------------------
1 | 1 | Alabama
1 | 2 | California
1 | 3 | Florida
1 | 4 | Hawaii
2 | 5 | London
2 | 6 | Oxford
我想根据列表对象创建JSON字符串。
我希望得到的JSON格式如下所示
{
1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
2: { '5': 'London', '6': 'Oxford' }
};
{
[
{"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对象
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方法时得到的实际输出如下所示
{
1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
2: { '5': 'London', '6': 'Oxford' }
};
{
[
{"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字符串格式?
每一个建议都将不胜感激。将其转换为指定格式的最简单方法是将自定义类转换为嵌套字典:
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)
}
将其转换为指定格式的最简单方法是将自定义类转换为嵌套字典:
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)
}
这样做的一个简单方法似乎是使用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似乎在键不是字符串的字典上失败…一种简单的方法是使用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在具有非字符串键的字典上似乎失败…您需要序列化字典,而不是列表,才能获得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; }
}
这使用了作为LINQ一部分的ToDictionary扩展方法
输出:
{"1":{"1":"Lzwuoge","2":"0lzpas"},"2":{"1":"Mqn3ul5k","2":"Kefzu"}}
要获得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; }
}
这使用了作为LINQ一部分的ToDictionary扩展方法
输出:
{"1":{"1":"Lzwuoge","2":"0lzpas"},"2":{"1":"Mqn3ul5k","2":"Kefzu"}}
调用时,您将什么对象传递到扩展方法中?@prashantht,我只是像var\u CountryNState=objRepository.CountryNState\u Select\u Lookup.CountryNState;///它是泛型列表对象_CountryNState.ToJSON;调用时,您将什么对象传递到扩展方法中?@prashantht,我只是像var\u CountryNState=objRepository.CountryNState\u Select\u Lookup.CountryNState;///它是泛型列表对象_CountryNState.ToJSON;你的回答帮助我很多,非常感谢@Leland Richardson你的回答帮助我很多,非常感谢@Leland Richardson