简体   繁体   中英

C# Deserialize JSON to Object

I have a little problem with C# program. It is my first program in C# so please - be forgiving :)

I would like to deserialize my json file into object, but I don't know how my class should be built. Im using Newtonsoft JSON lib.

JSON file:

http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json


Form1.cs:

    private void button1_Click(object sender, EventArgs e)
    {
        CurrencyCodeValues k = new CurrencyCodeValues();
        WebClient myWebClient = new WebClient();
        dynamic result = myWebClient.DownloadString("http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json");
        IList<CurrencyCodeValues> m = JsonConvert.DeserializeObject<CurrencyCodeValues>(result);
    }

Class1.cs:

class CurrencyCodeValues
{
    public string table { get; set; }
    public string no { get; set; }
    public string effectiveDate { get; set; }
    public List<rates_> rates { get; set; }

}

public class rates_
{
    public string currency { get; set; }
    public string code { get; set; }
    public float mid { get; set; }
}

Error message:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'WindowsFormsApplication4.CurrencyCodeValues' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

As your error message suggest your class should look like this

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string table { get; set; }
    public string no { get; set; }
    public string effectiveDate { get; set; }
    public Rate[] rates { get; set; }
}

public class Rate
{
    public string currency { get; set; }
    public string code { get; set; }
    public float mid { get; set; }
}

and then deseralize it like

JsonConvert.DeserializeObject<Rootobject>(result);

If you are not sure how your class model has to look like to match a specific json structure, Visual Studio (since VS2013 Update 2) has quite a helpful feature. You can copy your json string, go to Visual Studio and then click Edit -> Paste Special -> Paste JSON As Classes.
Now Visual Studio will make up a fitting class model.


In your case it will look like this (like Mohit Shrivastava already suggested):

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string table { get; set; }
    public string no { get; set; }
    public string effectiveDate { get; set; }
    public Rate[] rates { get; set; }
}

public class Rate
{
    public string currency { get; set; }
    public string code { get; set; }
    public float mid { get; set; }
}

while deserialising json you need to use basic structures and custom clases, because json does not suports lists, maps, and other structures that keeps data and does some logic at it. this means that you cannot use List<rates_> you need to use rates_[]

also as Mohit Shrivastava sugested - you need to add Wrapper class, because yout request returns array of CurrencyCodeValues objects, not single object

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