2013-08-24 76 views
1

所以我試圖使用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 有一點幫助,讓我在這一步將不勝感激。

+0

與套管相關嗎? JSON有小寫值,類是pascal ... –

+0

我有一種感覺,因爲元素是一個數組。現在我明白我該如何處理這個問題了。 –

回答

2

嘗試「(從JSON開始和「)」從最終消除jsonFlickrApi」 ..

我想你發送一個回調參數去API,你不需要在這裏,因爲你沒有使用JavaScript解析迴應

+0

好的我在修改JSON響應的開始和結尾之後將其轉換爲字符串......然後將其轉換回MemoryStream,將seek的位置設置爲0,以便在需要的情況下將其解析爲流DataContractJsonSerializer和...仍然沒有。調試器顯示數據在那裏,直到它通過串行器。 –

+0

我沒有使用RootObject,使用Photos元素是一個錯誤。最後,我的數據在jsonReponse對象中返回!感謝那個關於去除jsoncallback的問題。 –

+1

ahhh !!!你剛剛救了我男人!花費了很多時間來弄清楚問題所在。 +1爲最簡單但棘手的解決方案。 :d –