2017-09-21 15 views
2

私はとてもとしてinnerDictionaryとinnermostClassとouterDictionaryにDeserializeObjectしたいJSONを持っている:デシリアライズネストされたJSON文字列とクラス

var entityMap = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, fieldClass>>>(File.ReadAllText("map.json")); 

しかし、innerDictionaryは文字列を持っていることがあります。文字列の代わりに文字列:innerMostClass。

{ 
"Client": { 
    "__class__": "contact", 

    "ClientId": { 
     "__field__": "new_ndisclientid", 
     "__type__": "string" 
    }, 
    "GivenName": { 
     "__field__": "firstname", 
     "__type__": "string" 
    }, 
}, 
"Case": { 
    "__class__": "contact", 

    "CaseId": { 
     "__field__": "new_ndiscaseid", 
     "__type__": "string" 
    } 
} 
} 

これを行う方法はありますか?私はそのすべてをクラスにしたくありません。

これはカスタムJsonConverterで可能ですか?

EDIT:わかりやすくするために、classnameをentityNameに変更しました。 ClientIdとGivenNameは、fieldClassesにデシリアライズされます。

答えて

2

あなたは、動的使用するかは、私がダイナミック試してみたが、それは、クラスまたは似たようなことを好むだろう

public class Client 
{ 
    public string __entityName__ { get; set; } 
    public Dictionary<string, string> ClientId { get; set; } 
    public Dictionary<string, string> GivenName { get; set; } 
} 
var deserializedWithClass = JsonConvert.DeserializeObject<Dictionary<string, Client>>(json); 
+0

別の内部クラスを使用する場合は代わりに内部クラス

var json = "{\r\n\t\"Client\": {\r\n\t\t\"__entityName__\": \"contact\",\r\n\r\n\t\t\"ClientId\": {\r\n\t\t\t\"__field__\": \"new_ndisclientid\",\r\n\t\t\t\"__type__\": \"string\"\r\n\t\t},\r\n\t\t\"GivenName\": {\r\n\t\t\t\"__field__\": \"firstname\",\r\n\t\t\t\"__type__\": \"string\"\r\n\t\t}\r\n\t}\r\n}"; var deserialized = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, dynamic>>>(json); List<object> values = deserialized.SelectMany(result => result.Value).Cast<object>().ToList(); 

の対象とすることができ、可能なら。クラスにメソッドを持たせ、Visual Studioに動的データ型にIntelliSenseがないようにしたい。 –

+0

ダイナミックではなくクラスで回答を更新 – Abhay

+0

ルートをクラスにすることは別の方法です。しかし、私はそれが辞書/リストでなければならないと思うルートは常にクライアントではないかもしれません。それは、異なる内部クラスを持つが、同じフィールドを持つケースと言うことができます。私は元の投稿JSONを更新して、私が何を意味するのかを示しました。 –

関連する問題