所以我試圖使用DataContractJsonSerializer解析Flickr中的一些JSON數據 我收到JSON響應並將它保存在Stream中沒有問題,這意味着我通過傳遞它來測試它寫入文件和JSON都在那裏。使用DataContractJsonSerializer解析Flickr JSON響應
jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"[email protected]", "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 有一點幫助,讓我在這一步將不勝感激。
與套管相關嗎? JSON有小寫值,類是pascal ... –
我有一種感覺,因爲元素是一個數組。現在我明白我該如何處理這個問題了。 –