JSON からモデルを作成しようとしています。私は Newtonsoft.Json.JsonConvert を使用しています。
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(urlhere);
httpWebRequest.Credentials = new NetworkCredential("un", "pw");
httpWebRequest.Method = "Get";
httpWebRequest.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string incomingItemAsJson = streamReader.ReadToEnd(); // should be the json response as a string
MyObject myObject= JsonConvert.DeserializeObject<MyObject>(incomingItemAsJson);
Json の子要素にアクセス (および反復) するのに苦労しています。
{
"field1": "blah blah",
"field2": "blah blah",
"field3": [(2)
{
"field3Item1Id": "xxx",
"field3Item1Value": "Goo"
},-
{
"field3Item2Id": "xxx",
"field3Item2Value": "Foo"
}-
],-
"field4": {
"field4Id": xxx,
"field4Value": "Moo"
}-
}
次のように、この情報をモデルにマッピングしたいと思います。
public class MyObject
{
public string field1 {get;set;} //blah blah
public string field2 {get;set;} //blah blah
public string field3 {get;set;} //Goo, Foo
public string field4 {get;set;} //Moo
}
Field1 と field2 は MyObject の文字列プロパティに割り当てられています - 問題ありません。
私の質問:
フィールド3
「Goo, Foo」のように、各項目の値の部分を 1 つの文字列に連結するにはどうすればよいですか?
フィールド4
field4 には子要素が 1 つだけ存在します。
値の部分のみを抽出して MyObject の文字列プロパティにマップする簡単な方法はありますか?