我试图从URL
下载JSON
数据,之后我想将该数据绑定到列表。我可以获得JSON数据,但我无法将它们绑定到我创建的类的对象。我该怎么做呢?我创建的类的成员与JSON string的成员相同。
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。在你的例子中,它可以是这样的:
class GetModel
{
public IEnumerable<Current> Currents { get; set; }
}
和反序列化:
var model = JsonConvert.DeserializeObject<GetModel>(json);
您可以试试这段代码。
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) { }
}
}
给你,两种方式
dynamic d = JObject.Parse(json_data)
或类////new up your list
List myList = new List<Current>();
/////deserialize
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; }
}
CurrentCode不匹配
你可以通过修饰你的公共属性来帮助反序列化器。
[JsonProperty("currentCode")]
public String CurrentCode
{
get { return currentCode; }
set { currentCode = value; }
}
结合Alex的回答,你应该没问题。