2014-07-22 103 views
0

我正試圖從URL下載JSON數據,之後我想將這些數據綁定到列表中。我可以獲取JSON數據,但我無法將它們綁定到我創建的類的對象。我怎樣才能做到這一點?我創建的類的成員與JSON字符串的成員相同。如何使用JSON.NET將JSON對象(JSON字符串)反序列化爲C#對象列表?

public void getCurrentsJSON(String url) 
{ 
    using (var w = new WebClient()) 
    { 
     var json_data = string.Empty; 
     // attempt to download JSON data as a string 
     try 
     { 
      json_data = w.DownloadString(url); 
      Current currents = JsonConvert.DeserializeObject<Current>(json_data); 
     } 
     catch (Exception) { } 
    }  
} 

當前等級:

public class Current 
    { 
     private String currentCode; 
     private String currentName; 
     private String currentAddress; 
     private String currentTel; 
     private String fax; 
     private String currentProvince; 
     private String currentCounty; 
     private String taxOffice; 
     private String taxNo; 
     private String currentType; 
     private String postalCode; 
     private String countryCode; 
     private String additionalCurrentCode; 

     public String CurrentCode 
     { 
      get { return currentCode; } 
      set { currentCode = value; } 
     } 

     public String CurrentName 
     { 
      get { return currentName; } 
      set { currentName = value; } 
     } 

     public String CurrentAddress 
     { 
      get { return currentAddress; } 
      set { currentAddress = value; } 
     } 

     public String CurrentTel 
     { 
      get { return currentTel; } 
      set { currentTel = value; } 
     } 

     public String Fax 
     { 
      get { return fax; } 
      set { fax = value; } 
     } 

     public String CurrentProvince 
     { 
      get { return currentProvince; } 
      set { currentProvince = value; } 
     } 

     public String CurrentCounty 
     { 
      get { return currentCounty; } 
      set { currentCounty = value; } 
     } 

     public String TaxOffice 
     { 
      get { return taxOffice; } 
      set { taxOffice = value; } 
     } 

     public String TaxNo 
     { 
      get { return taxNo; } 
      set { taxNo = value; } 
     } 

     public String CurrentType 
     { 
      get { return currentType; } 
      set { currentType = value; } 
     } 

     public String PostalCode 
     { 
      get { return postalCode; } 
      set { postalCode = value; } 
     } 

     public String CountryCode 
     { 
      get { return countryCode; } 
      set { countryCode = value; } 
     } 

     public String AdditionalCurrentCode 
     { 
      get { return additionalCurrentCode; } 
      set { additionalCurrentCode = value; } 
     } 

    } 

JSON數據:

{ 
    "currents": [ 
     { 
      "currentCode": 1, 
      "currentName": "Current1", 
      "currentAddress": "CurrentAdress1", 
      "currentTel": "CurrentTel1", 
      "fax": "Fax1", 
      "currentProvince": "CurrentProvince1", 
      "currentCounty": "CurrentCounty1", 
      "taxOffice": "TaxOffice1", 
      "taxNo": "TaxNo1", 
      "currentType": "CurrentType1", 
      "postalCode": "PostalCode1", 
      "countryCode": "CountryCode1", 
      "additionalCurrentCode": 1 
     } 
    ] 
} 
+0

張貼您的json和您的當前類 – saj

+0

@saj檢查它,我編輯了這個問題。 – Tartar

回答

0

你可以試試這個代碼。

public void getCurrentsJSON(String url) 
{ 
using (var w = new WebClient()) 
{ 
    var json_data = string.Empty; 
    // attempt to download JSON data as a string 
    try 
    { 
     json_data = w.DownloadString(url); 
     List<Current> currents = JsonConvert.DeserializeObject<List<Current>>(json_data); 
    } 
    catch (Exception) { } 
}  
} 
+0

這不適合我。 – Tartar

+0

將你的屬性裝飾到相應的json字段字段 –

2

您需要創建一個模型來描述您獲得的JSON。 在你的情況可能是類似的東西:

class GetModel 
{ 
    public IEnumerable<Current> Currents { get; set; } 
} 

和反序列化:

var model = JsonConvert.DeserializeObject<GetModel>(json); 
+0

同樣的結果。當我嘗試使用foreach顯示時,我無法看到任何對象 – Tartar

+0

這對我不起作用。 – Tartar

+0

這很奇怪,我只是試着用你的數據來做,而且對我很有用。反序列化'model'具有'Currents'屬性和一個'Current',其中所有屬性都被填充。也許你在項目的某個地方設置了一些全局的JSON.NET設置,導致錯誤的反序列化? –

0

在這裏你去,2種方式

dynamic d = JObject.Parse(json_data) 

或類 ////新up your list

List myList = new List<Current>(); 

///// deserialise

Current currents = JsonConvert.DeserializeObject<RootObject>(json_data);  


public class Current 
{ 
public int currentCode { get; set; } 
public string currentName { get; set; } 
public string currentAddress { get; set; } 
public string currentTel { get; set; } 
public string fax { get; set; } 
public string currentProvince { get; set; } 
public string currentCounty { get; set; } 
public string taxOffice { get; set; } 
public string taxNo { get; set; } 
public string currentType { get; set; } 
public string postalCode { get; set; } 
public string countryCode { get; set; } 
public int additionalCurrentCode { get; set; } 
} 

public class RootObject 
{ 
public List<Current> currents { get; set; } 
} 
+0

我不想使用額外的類來實現此目的。我會盡力找到另一種方式。 – Tartar

+0

更新爲使用動態動態d = JObject.Parse(json_data) – saj

+0

如果選擇使用動態,將使用哪種對象類型?我需要將這些json數據轉換爲c#對象,這沒有任何意義。 – Tartar

0

CurrentCode不會匹配currentCode

您可以裝飾你的公共屬性幫助解串器。

[JsonProperty("currentCode")] 
public String CurrentCode 
    { 
     get { return currentCode; } 
     set { currentCode = value; } 
    } 

與Alex的答案相結合,你應該是好的。

相關問題