我将要反序列化的JSON文件结构如下:
{
"id" : "1lad07",
"text" : "test",
"url" : "http:\/\/twitpic.com\/1lacuz",
"width" : 220,
"height" : 84,
"size" : 8722,
"type" : "png",
"timestamp" : "Wed, 05 May 2010 16:11:48 +0000",
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
}
我创建了一个类,其字段名作为JavaScriptSerializer属性。我将使用以下代码对JSON进行反序列化:
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
var responseBody = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Response>(responseBody);
}
我的问题是如何读取JSON文件中的用户字段,例如下面的内容;
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
它有子属性和值。我该如何在我的响应类中为它们命名。我的响应类现在看起来像这样:
public class Response {
public string id { get; set; }
public string text { get; set; }
public string url { get; set; }
public string width { get; set; }
public string height { get; set; }
public string size { get; set; }
public string type { get; set; }
public string timestamp { get; set; }
}
最好的情况是怎样做?