简体   繁体   中英

How to deserialize JSON Object (JSON string) to a C# object list using JSON.NET?

I am trying to download JSON data from a URL and after that I want to bind that data to a list. I can get the JSON data but I am unable to bind them to the object of the class I created. How can I do this? The members of the class I created are same as those of 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) { }
    }    
}

Current Class:

 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 Data:

{
    "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
        }
    ]
}

You need to create a model that will describe JSON that you get. In your case it can be something like that:

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

And deserialization:

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

You Can try this code.

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) { }
}    
}

Here you go, 2 ways

dynamic d = JObject.Parse(json_data)

or with classes //// new 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; }
 }

CurrentCode will not match to currentCode

You can help the deserializer by decorating your public properties.

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

combine that with Alex's answer and you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM