因此,我尝试使用 DataContractJsonSerializer 解析来自 Flickr 的一些 JSON 数据我正在接收 JSON 响应并将其保存在 Stream 中没有问题,这意味着我已经通过写入文件传递它来测试它,并且 JSON 就在那里。
jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"7960563@N07", "secret":"b7b80b75f8", "server":"3734", "farm":4, "title":"1970 - 1978 Toyota Corolla E20 Coup\u00e9", "ispublic":1, "isfriend":0, "isfamily":0, "url_t":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_b7b80b75f8_t.jpg", "height_t":"67", "width_t":"100", "url_o":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_0eda23bccb_o.jpg", "height_o":"1000", "width_o":"1500"}}]}, "stat":"ok"})
但是当我尝试使用 Jsonserializer 进行解析时,我注意到它不包含任何内容。
感谢 Json2Cshap.com,我已按合同类建立
public class ResponseContract
{
[DataContract]
public class Photo
{
[DataMember]
public string id { get; set; }
[DataMember]
public string owner { get; set; }
[DataMember]
public string secret { get; set; }
[DataMember]
public string server { get; set; }
[DataMember]
public int farm { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public string url_t { get; set; }
[DataMember]
public string url_o { get; set; }
}
[DataContract]
public class Photos
{
[DataMember]
public int page { get; set; }
[DataMember]
public int pages { get; set; }
[DataMember]
public int perpage { get; set; }
[DataMember]
public string total { get; set; }
[DataMember]
public List<Photo> photoList { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Photos photos { get; set; }
[DataMember]
public string stat { get; set; }
}
我的代码如下所示:
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.longUrl);
// Send the request and wait for response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the response stream
Stream responseStream = response.GetResponseStream();
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Photos));
object objResponse = (Photos)jsonSerializer.ReadObject(responseStream);
Photos jsonResponse = objResponse as Photos;
response.Close();
但我在 jsonResponse 中一无所获
从 msdn.microsoft.com/en-us/library/hh674188.aspx 获得了一些代码如果能帮助我完成这一步,将不胜感激。