我正試圖從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
}
]
}
張貼您的json和您的當前類 – saj
@saj檢查它,我編輯了這個問題。 – Tartar