Generate model for the response of an api

0

At the moment of consuming an api, the next response returns.

[
    {
         "MiKey": 0000,
         "MiKey": 02,
         "MiKey": "CADENA",
    }
]

Fictitious data

My Model:

public class RootObject
{
    public int MiKey { get; set; }
    public int MiKey { get; set; }
    public string MiKey { get; set; }
}

Fictitious data

But at the time of doing the await with this model, it returns the following error.

  

Can not deserialize the current JSON array (eg [1,2,3]) into type 'DetamaticWeb.Models.RootOb' 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.

What would be the model to generate correctly? Since from what I see the model or the data output is the one giving the problem.

    
asked by vcasas 24.05.2018 в 14:54
source

1 answer

1

The Visual Studio IDE has a given function of Json generating C # classes. Select the Json , copy it to the clipboard and go to the menu Edit\Paste special\Paste JSON as classes . I put it in English because that's how I have it configured. This gives us a model like this

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

public class Class1
{
    public string MiKey { get; set; }
}

Everything is a string since both 0000 as 02 are not numeric values as an integer.

    
answered by 24.05.2018 / 23:53
source