2015-04-04 38 views
0

我有一個Api鏈接,返回這個Json structure。在代碼中,我請求這個Api鏈接,然後將其反序列化爲一個列表。這沒有問題。但是如果Api返回超過50個「計數」,它會創建另一個頁面。我如何繞過所有頁面並將所有內容添加到現有列表中?c#反序列化多重Json到同一個列表

在我鏈接的情況下會有38頁。所有需要被添加到列表中。

呼叫

// spidyApiUrl = http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1 
var spidyApi_idByName_result = api_Handler.objFromApi_idToName(spidyApiUrl); 

功能與回報

public RootObject objFromApi_idToName(string url) 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

      WebResponse response = request.GetResponse(); 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
       var jsonReader = new JsonTextReader(reader); 
       var serializer = new JsonSerializer(); 
       //return serializer.Deserialize<RootObject>(jsonReader); 
       RootObject rootObject = serializer.Deserialize<RootObject>(jsonReader); 

       if (rootObject.count > 0) { return rootObject; } 
       else { return null; } 

      } 

     } 

而且ofcourse我也有get; set;類。

如何遍歷所有頁面(如果存在多個頁面,則不需要)並將它們添加到同一個對象列表中。

回答

1

你需要不斷下載數據,直到page == last_page 當你每個頁面的數據,你再添加一組新的results原來的rootObject的results財產與的AddRange

我也改變了被傳遞到的網址功能從

http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1 

http://www.gw2spidy.com/api/v0.9/json/item-search/iron 

這使我的頁碼添加到URL來獲取所有

頁面
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1 
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/2 
..... 
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/38 

代碼:

public class Result 
{ 
    public int data_id { get; set; } 
    public string name { get; set; } 
    public int rarity { get; set; } 
    public int restriction_level { get; set; } 
    public string img { get; set; } 
    public int type_id { get; set; } 
    public int sub_type_id { get; set; } 
    public string price_last_changed { get; set; } 
    public int max_offer_unit_price { get; set; } 
    public int min_sale_unit_price { get; set; } 
    public int offer_availability { get; set; } 
    public int sale_availability { get; set; } 
    public int sale_price_change_last_hour { get; set; } 
    public int offer_price_change_last_hour { get; set; } 
} 

public class RootObject 
{ 
    public int count { get; set; } 
    public int page { get; set; } 
    public int last_page { get; set; } 
    public int total { get; set; } 
    public List<Result> results { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     objFromApi_idToName("http://www.gw2spidy.com/api/v0.9/json/item-search/iron"); 
    } 

    public static RootObject objFromApi_idToName(string url) 
    { 
     RootObject rootObject = null; 
     RootObject tempRootObject = null; 
     int page = 1; 
     do 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/" + page); 

      WebResponse response = request.GetResponse(); 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
       var jsonReader = new JsonTextReader(reader); 
       var serializer = new JsonSerializer(); 
       //return serializer.Deserialize<RootObject>(jsonReader); 
       tempRootObject = serializer.Deserialize<RootObject>(jsonReader); 

       if (rootObject == null) 
       { 
        rootObject = tempRootObject; 
       } 
       else 
       { 
        rootObject.results.AddRange(tempRootObject.results); 
        rootObject.count += tempRootObject.count; 
       } 
      } 
      page++; 
     } while (tempRootObject != null && tempRootObject.last_page != tempRootObject.page); 
     return rootObject; 
    } 
} 
+0

謝謝你的迴應!這將返回一個'遠程服務器返回一個錯誤:(404)Not Found.' on WebResponse response = request.GetResponse();'但我不知道爲什麼,我檢查錯誤是否正確。 – 2015-04-04 18:37:41

+1

我將起始網址更改爲「http://www.gw2spidy.com/api/v0.9/json/item-search/iron」而沒有「/ 1」 在函數I中,然後添加我需要的頁碼,這可能是爲什麼你會得到一個錯誤 – Martin 2015-04-04 18:40:22

+0

是的,它現在完美的工作!的確,這個網址是不正確的 – 2015-04-04 18:45:46

0

您正在使用的Web API?如果是這樣,你可以嘗試這樣的事情嗎?

public RootObject objFromApi_idToName(string url) 
    { 
     using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri("<your uri here>"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      HttpResponseMessage response = await client.GetAsync("<uri extention here>"); 
      if (response.IsSuccessStatusCode) 
      { 
       string jsonStr = await response.Content.ReadAsStringAsync(); 
       var deserializedResponse = JsonConvert.DeserializeObject<List<your model class here>>(jsonStr); 
       return deserializedResponse; 
     } 
    }