JSONデータの文字列をC#クラスオブジェクトに変換しようとしています。しかし、私は本質的に動的なJSONの小さな部分に問題があります。Json DeserializeObjectを動的プロパティを持つクラスに追加する
JSONの一部は、以下である:
"contact": [{
"comment": null,
"type": {
"id": "cell",
"name": "Example name"
},
"preferred": true,
"value": {
"country": "7",
"formatted": "+7 (702) 344-3423-3",
"number": "3498908",
"city": "702"
}
},
{
"type": {
"id": "email",
"name": "Email example"
},
"preferred": false,
"value": "[email protected]"
}]
C#クラス
public class Value
{
public string country { get; set; }
public string formatted { get; set; }
public string number { get; set; }
public string city { get; set; }
}
public class Type
{
public string id { get; set; }
public string name { get; set; }
}
public class Contact
{
public string comment { get; set; }
public Type type { get; set; }
public bool preferred { get; set; }
public string value { get; set; }
}
C#コード
Contact contact = JsonConvert.DeserializeObject<Contact>(result);
接触情報に応じて、 "値" の変更の形式。値を文字列とクラス値の両方にマップすることは可能ですか?
ご協力いただきありがとうございます。あなたがJSONで見ることができるように
私には、クラス構造が間違っているようです。基本クラスValueと他の2つのクラスAddressValueとEmailValueがあるように見えます。 –