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を返します

注: JavaScriptSerializer が文字列ではないキーを持つ辞書で失敗するように見えるため、辞書キーで .ToString() を呼び出す必要があります...

于 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 に答える