简体   繁体   中英

Put JSON data into c# datatable

I am trying to deserialise some JSON that I get back from an API so that I can loop through an array of county names and add the information to a datatable in C#. However I am receiving following error at the first hurdle when I try and deserialise it:

error: System.MissingMethodException: No parameterless constructor defined for type of 'DPDJSONLibrary.DPD_JSON+LOCR_Data[]'.

The provider of the API provides an example of the JSON response as follows:

{
    "error": null,
    "data":[{
        "country": [{
            "countryCode":"GB",
            "countryName":"United Kingdom",
            "internalCode":"UK",
            "isEUCountry":false,
            "isLiabilityAllowed":false,
            "isoCode":"826",
            "isPostcodeRequired":false,
            "liabilityMax":15000
        }]
    }]
}

A sample of the JSON data I am getting back from the API is:

{
    "data": {
        "country":[
            {
                "countryCode":"PM",
                "countryName":"St Pierre & Miquilon",
                "isoCode":"666",
                "isEUCountry":false,
                "isLiabilityAllowed":true,
                "liabilityMax":15000,
                "isPostcodeRequired":true
            },
            {
                "countryCode":"SR",
                "countryName":"Suriname",
                "isoCode":"740",
                "isEUCountry":false,
                "isLiabilityAllowed":true,
                "liabilityMax":15000,
                "isPostcodeRequired":true
            },
            {
                "countryCode":"SZ",
                "countryName":"Swaziland",
                "isoCode":"748",
                "isEUCountry":false,
                "isLiabilityAllowed":true,
                "liabilityMax":15000,
                "isPostcodeRequired":true
            }
        ]
    }
}

I have tried to make some classes to put the JSON in as follows:

/// <summary>
/// List Of Countries Response object.
/// </summary>
public class LOCR
{
    public LOCR_Error error { get; set; }
    public LOCR_Data[] data { get; set; }
}

public class LOCR_Error
{
    public string errorAction { get; set; }
    public string errorCode { get; set; }
    public string errorMessage { get; set; }
    public string errorObj { get; set; }
    public string errorType { get; set; }
}

public class LOCR_Data
{
    public LOCR_Data_Country[] country { get; set; }
}

public class LOCR_Data_Country
{
    public string countryCode { get; set; }
    public string countryName { get; set; }
    public string internalCode { get; set; }
    public bool isEUCountry { get; set; }
    public bool isLiabilityAllowed { get; set; }
    public string isoCode { get; set; }
    public bool isPostcodeRequired { get; set; }
    public int liabilityMax { get; set; }
}

When I get the JSON back as a string, I am trying to use the Newtonsoft (plugin?) to put it into my classes using:

JavaScriptSerializer ser = new JavaScriptSerializer();
DPD_JSON.LOCR DPDCountries = new DPD_JSON.LOCR();
DPDCountries = ser.Deserialize<DPD_JSON.LOCR>(data);

It is the last line above that is generating the error. I suspect I've written my classes wrong that I am trying to deserialise the JSON in to - can anyone see where I've gone wrong?

Deserialize will return a list and not an array, So your LOCR_Data_Country should be of type List and not array:

    public class LOCR_Data
    {
        public List<LOCR_Data_Country> country { get; set; }
    }

There's a HUGE difference between the two example JSON strings you've shown. Mainly the first one is an array : "data":[ ... ] and the second one is an object "data:{ ... } . These two are not interchangeable so you have to stick to either one of those. If the thing you're getting back from the API is an object instead you should rewrite your model to be :

public class LOCR
{
    public LOCR_Error error { get; set; }
    // object here since "data": { ... }
    public LOCR_Data data { get; set; }
}

And as you move further with the JSON you can see that LOCR_Data.country is in fact an array in both cases "country": [ ... ] so you can stick with the current implementation of LOCR_Data class.

Try Using :

YourResultClass object = JsonConvert.DeserializeObject<YourResultClass>(Jsonstring);

See the answer of this Using JsonConvert.DeserializeObject to deserialize Json

OR

dynamic data = Json.Decode(json);

You can refer this Deserialize JSON into C# dynamic object? for further assistance

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