2012-06-21 74 views
1

我有一個Generic List對象,該對象在表格下面保存爲自己的值。使用自定義格式序列化JSON字符串

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' } 
}; 

我用下面的類來生成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字符串格式,因爲我想?
每個建議將不勝感激。

+1

打電話時,你通過什麼對象到您的擴展方法? –

+0

@prashantht,我只是調用我的擴展方法,就像'var _CountryNState = objRepository.CountryNState_Select_Lookup()。CountryNState; ///這是泛型列表對象 _CountryNState.ToJSON();' –

回答

3

這似乎是一個簡單的方法來做到這一點是使用LINQ選擇到字典中,然後發送字典對象的JavascriptSerializer ...

priveded我們:

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似乎未能與該字符串的arent鍵Dictionarys ...

+0

你回答我很多,非常感謝你@Leland Richardson –

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) 
} 
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"}}