0

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
        }
    ]
}
2
  • post your json and your Current class
    – fuzzybear
    Commented Jul 22, 2014 at 11:44
  • @saj check it, i have edited the question.
    – Tartar
    Commented Jul 22, 2014 at 11:47

4 Answers 4

2

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);
4
  • same result. I couldnt see any object when i try to display by using foreach
    – Tartar
    Commented Jul 22, 2014 at 11:54
  • This is strange, I just tried to do it with your data, and it works for me. Deserialized model has Currents property with one Current with all properties filled. Maybe you have some global JSON.NET settings set somewhere in the project, that causes wrong deserialization? Commented Jul 22, 2014 at 12:06
  • The list seems empty when i debug the program. I did not change any JSON.NET settings.
    – Tartar
    Commented Jul 22, 2014 at 12:08
  • Then my only advice for you, is to create new ConsoleApp and create really simple JSON string and model (keep the current structure) and try deserialization there. Try different casing for properties. Maybe you are using some old version of JSON.NET that can't map camel case JSON properties to pascal case C# class properties? Commented Jul 22, 2014 at 12:17
0

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) { }
}    
}
1
  • Decorate your properties to its corresponding json field fields Commented Jul 23, 2014 at 12:40
0

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; }
 }
8
  • I do not want to use an extra class to make this happen. I will try to find an another way.
    – Tartar
    Commented Jul 22, 2014 at 12:26
  • Which object type will be used if chose to use dynamic ? I need to convert these json data to c# objects and this does not make any sense.
    – Tartar
    Commented Jul 22, 2014 at 12:35
  • if you don't want to use classes, you so should use dynamic, at this point, it does not know what object type you json is, for example a json, object, array or string, you can convert to specific types if your sure what your type is always going to be for example JArray jsonVal = JArray.Parse(jsonString) as JArray; but if you only get 1 item back then can you see why dynamic makes more sense?
    – fuzzybear
    Commented Jul 22, 2014 at 12:39
  • I need to map that jeson data to my c# object(Current). So i think it is not usefull for me.
    – Tartar
    Commented Jul 22, 2014 at 12:41
  • You can't just map your JSON to Current because your JSON has a root object and there is an array of Current. Commented Jul 22, 2014 at 12:58
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.