2015-04-02 187 views
0

我有一個訪問API並檢索一些Json值的函數,這些值被返回並粘貼到一個富文本框中。我怎麼能夠將這個Json轉換成對象列表?互聯網充滿了我所要求的,但在閱讀並嘗試一切後,我並沒有明智之舉。c#Json到對象列表

這是函數,URL是檢索Json的Api鏈接。

public string GetApi(string url) 
     { 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

      try 
      { 
       WebResponse response = request.GetResponse(); 
       using (Stream responseStream = response.GetResponseStream()) 
       { 
        StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
        return reader.ReadToEnd(); 
       } 
      } 
      catch (WebException ex) 
      { 
       WebResponse errorResponse = ex.Response; 
       using (Stream responseStream = errorResponse.GetResponseStream()) 
       { 
        StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 
        String errorText = reader.ReadToEnd(); 
        // log errorText 
       } 
       throw; 
      } 
     } 

這是JSON結構

{"id":19684,"buys":[{"listings":10,"unit_price":94,"quantity":2498},{"listings":42,"unit_price":93,"quantity":10398},{"listings":139,"unit_price":92,"quantity":34501},{"listings":8,"unit_price":91,"quantity":1939},{"listings":38,"unit_price":90,"quantity":9270},{"listings":7,"unit_price":89,"quantity":1266},{"listings":43,"unit_price":88,"quantity":10565},{"listings":23,"unit_price":87,"quantity":5476},{"listings":80,"unit_price":86,"quantity":19827}, 
+0

檢索JSON的代碼與將JSON轉換爲列表的問題無關。你有什麼特別的嘗試做轉換?這將更貼切地發佈... – har07 2015-04-02 00:51:40

+0

@ har07我不斷被指向序列化,否則使用結構。 – 2015-04-02 00:59:39

+0

序列化有什麼問題?避免序列化的任何特定原因?這是一種乾淨的方式恕我直言,通過序列化將您的JSON轉換爲更友好的.NET對象 – har07 2015-04-02 01:09:22

回答

1

業務的第一步是使用一個庫來解析JSON。對於這個答案,我使用了.NET最常用的JSON庫之一Newtonsoft.Json

然後,應該識別從服務器收到的文檔的結構。我只是在這裏猜測,但我認爲它就像下面定義的類一樣。 JsonProperty是一個屬性,用於指定映射到該屬性的JSON文檔中的字段名稱。只有當您的屬性名稱與JSON文檔中的字段名稱不同時,才需要指定它。在這種情況下,unit_price與屬性的標準.NET PascalCase命名約定不匹配。

public class Listings 
{ 
    [JsonProperty(PropertyName = "id")] 
    public int Id { get; set; } 

    public List<Buy> Buys { get; private set; } 

    public Listings() 
    { 
     Buys = new List<Buy>(); 
    } 
} 
public class Buy 
{ 
    [JsonProperty(PropertyName = "listings")] 
    public int Listings { get; set; } 

    [JsonProperty(PropertyName = "unit_price")] 
    public int UnitPrice { get; set; } 

    [JsonProperty(PropertyName = "quantity")] 
    public int Quantity { get; set; } 
} 

一旦你定義了類結構,你可以讓庫完成所有的工作。查看文檔以獲取更多詳細信息,以下是您將如何檢索已寫入方法的列表。

public Listings GetApi(string url) 
{ 
    ... 
     using (Stream responseStream = response.GetResponseStream()) 
     { 
      StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
      var jsonReader = new JsonTextReader(reader); 
      var serializer = new JsonSerializer(); 
      return serializer.Deserialize<Listings>(jsonReader); 
     } 
    ... 
} 
+0

如何訪問我的其他主類中的對象? – 2015-04-02 01:27:08

+0

@KrijnvanderBurg你如何在你的其他主類中訪問你的字符串?這是方法調用的結果。 – Bas 2015-04-02 01:27:47

+0

你的意思是我如何訪問Json字符串?我用這個值作爲'api_Request.GetApi(「https://api.guildwars2.com/v2/commerce/listings/19684」);' – 2015-04-02 01:30:30