2017-02-26 4 views
1

私は次のように辞書を持っている:私ので、ネストされたキーと値のペアに変換、その後、C#の辞書キーをJSONのキー値に分割しますか? C#ので

"User": { "Info": "Your info", "Profile": "Profile" }, "Menu": { "System": { "Task": "Tasks" } } } 

分割ドットによるキー、 :

Dictionary<string,string> dict=new Dictionary<string,string>(); 
dict.Add("User.Info","Your info"); 
dict.Add("User.Profile","Profile"); 
dict.Add("Menu.System.Task","Tasks"); 
var output=dict??? 
return Json(output); 

そして、私はのようなものを変更したいです?ここにangular2ための言語リソースファイルをやっM、及びこれをarchieveする方法があるおかげ

答えて

0

は、複数のレベルを使用して経由で一つの方法である:

 Dictionary<string, object> dict = new Dictionary<string, object>(); 

     var user = new Dictionary<string, string>(); 
     user.Add("Info", "Your info"); 
     user.Add("Profile", "Profile"); 

     var menu = new Dictionary<string, object>(); 

     var system = new Dictionary<string, string>(); 
     system.Add("Task", "Tasks"); 

     menu.Add("System", system); 

     dict.Add("User", user); 
     dict.Add("Menu", menu); 

     string output = JsonConvert.SerializeObject(dict); 

     Console.WriteLine(output); 

出力:

{ "ユーザー":{ "情報": "あなたの 情報"、 "プロフィール": "プロフィール"}、 "メニュー":{ "システム":{ "タスク":" Tasks "}}}

p/s:この例を実行するには、参照番号Newtonsoft.Jsonを追加する必要があります。参照リンク:Serializing and Deserializing JSON

0

多分あなたができる「キーレベル」の任意の数(ドットの任意の数)

機能をサポートし、あなたがこれを行うことができ再帰関数を使う「System.Dynamic.ExpandoObject」について

+1

これは、コメントではない答えなければなりません。 –

2

を表示するには:

private static void SetValues(string[] keys, int keyIndex, string value, IDictionary<string, object> parentDic) 
{ 
    var key = keys[keyIndex]; 

    if (keys.Length > keyIndex + 1) 
    { 
     object childObj; 
     IDictionary<string, object> childDict; 
     if (parentDic.TryGetValue(key, out childObj)) 
     { 
      childDict = (IDictionary<string, object>)childObj; 
     } 
     else 
     { 
      childDict = new Dictionary<string, object>(); 
      parentDic[key] = childDict; 
     } 

     SetValues(keys, keyIndex + 1, value, childDict); 

    } 
    else 
    { 
     parentDic[key] = value; 
    } 
} 

例:

Dictionary<string, string> dict = new Dictionary<string, string>(); 
dict.Add("User.Info", "Your info"); 
dict.Add("User.Profile", "Profile"); 
dict.Add("Menu.System.Task", "Tasks"); 
dict.Add("Menu.System.Configuration.Number", "1"); 
dict.Add("Menu.System.Configuration.Letter", "A"); 

var outputDic = new Dictionary<string, object>(); 

foreach (var kvp in dict) 
{ 
    var keys = kvp.Key.Split('.'); 
    SetValues(keys, 0, kvp.Value, outputDic); 
    } 

    var json = JsonConvert.SerializeObject(outputDic); 

出力:

{ 
    "User": { 
    "Info": "Your info", 
    "Profile": "Profile" 
    }, 
    "Menu": { 
    "System": { 
     "Task": "Tasks", 
     "Configuration": { 
      "Number": "1", 
      "Letter": "A" 
     } 
    } 
    } 
} 
+0

偉大ですが、最終的に私は問題があることがわかりましたそれぞれのレベルの数が同じでなければなりません、ex dict.Add( "User.Profile"、 "Profile"); dict.Add( "User.Profile.Title"、 "Your profile"); これはエラーが発生するので、最後にすべてのドットを削除して、単純な文字列を使用します。 – protoss

+0

与えられた例では構造体が作成されませんでしたか?値が "profile"のプロパティuser.profileと、titleや "your profile"のような値の辞書を持ったプロパティuse.profileが必要でしょうか? – wdavo

2

JsonConverterのWriteJsonメソッドをオーバーライドすることで実現できます。

class CustomJsonConverter : JsonConverter 
    { 

     public override bool CanConvert(Type objectType) 
     { 
      bool result = typeof(Dictionary<string,string>).IsAssignableFrom(objectType); 
      return result; 
     } 

     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
     { 
      JObject jo = new JObject(); 

      foreach (var item in (Dictionary<string,string>)value) 
      { 
       if (item.Key.Contains(".")) 
       { 
        if (jo.Property(item.Key.Split('.')[0].ToString()) == null) 
        { 
         jo.Add(item.Key.Split('.')[0], 
           new JObject() { { item.Key.Split('.')[1], item.Value } }); 
        } 
        else 
        { 
         var result = jo.Property(item.Key.Split('.')[0].ToString()).Value as JObject; ; 
         result.Add(item.Key.Split('.')[1], item.Value); 
        } 
       } 
       else 
       { 
        jo.Add(item.Key, item.Value); 
       } 
      } 
      jo.WriteTo(writer); 
     } 

     public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

例:

Dictionary<string, string> dict = new Dictionary<string, string>(); 
    dict.Add("User.Info", "Your info"); 
    dict.Add("User.Profile", "Profile"); 
    dict.Add("Menu.System.Task", "Tasks"); 

    JsonSerializerSettings obj = new JsonSerializerSettings(); 
    obj.Converters.Add(new CustomJsonConverter()); 

    var output1 = JsonConvert.SerializeObject(dict,obj); 

出力:

{"User":{"Info":"Your info","Profile":"Profile"},"Menu":{"System":"Tasks"}} 
関連する問題