Deserialize JSon


I have a problem when dezializing a json that I downloaded from a service.

The structure of the json is as follows:

    "{\"d\":{\"tipo\":\"valor\",\"Id\":valor,\"Result\":valor,\"d\":\{\\"Nombre\\":[{\\"idnombre1\\":valor,\\"idnombre2\\":\\"valor\\", \\"idnombre3\\":\\"valor\\", \\"idnombre4\\":\\"valor\\"}]\\"baja\\":valor, \\"baja2\\":[]}\"}}"

I receive this json from a task, and I put it in a string. It looks like this:

    string resultadoJson = prueba.Entrar("parametroacceder1","parametroacceder2").Result;

And then I try to deserialize it:

    RootObject objClass = JsonConvert.DeserializeObject<RootObject>(resultadoJson);

And I have the following structure

    public class Nombre
       public string idnombre1 { get; set; }
       public string idnombre2{ get; set; }
       public string idnombre3{ get; set; }
       public string idnombre4{ get; set; }
    public class Datum
       public List<Nombre> program { get; set; }
    public class RootObject
       public int Id { get; set; }
       public int result { get; set; }
       public List<Datum> d { get; set; }
       public string baja { get; set; }

When the program is going to deserialize I get an error with the JSON

Can not deserialize the current JSON object because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Can you give me a hand to know what I'm doing wrong?

Thank you.

asked by Zarios 04.02.2017 в 20:48

2 answers


SPANISH : HELLO =) ... I use this page, normally to create a JSON class and that the format is correct, we present an example case of a JSON and the class as it should be at the end

i use this page to create a perfect Class from json


example in this case, i have a json

    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    "GlossSee": "markup"

and the result of this class is:

public class GlossDef
    public string para { get; set; }
    public List<string> GlossSeeAlso { get; set; }

public class GlossEntry
    public string ID { get; set; }
    public string SortAs { get; set; }
    public string GlossTerm { get; set; }
    public string Acronym { get; set; }
    public string Abbrev { get; set; }
    public GlossDef GlossDef { get; set; }
    public string GlossSee { get; set; }

public class GlossList
    public GlossEntry GlossEntry { get; set; }

public class GlossDiv
    public string title { get; set; }
    public GlossList GlossList { get; set; }

public class Glossary
    public string title { get; set; }
    public GlossDiv GlossDiv { get; set; }

public class RootObject
    public Glossary glossary { get; set; }
answered by 04.02.2017 / 21:01

Reviewing your json I see that it is not well formed, since it does not have a correct structure, for example in this code section ":\{\\"Nombre\\":[{\

I took as a base the structure of classes that you have defined (to be able to replicate the scenario) and a json is required, very similar to this one (at least in terms of the structure):

    "Id": 1,
    "result": 2,
    "d": [{
            "program": [{
                    "idnombre1": "id nombre 1",
                    "idnombre2": "id nombre 2",
                    "idnombre3": "id nombre 3",
                    "idnombre4": "id nombre 4"
    "baja": "1"

Here, the solution would be, what adjustments your class structure makes up the json obtained, or in your case, the json have the structure of your classes defined.

Note: it would be more helpful if you edit your question and put the correct structure of json , with this, we can know exactly what type of structure you have and we could give you a more timely response.

answered by 05.02.2017 в 05:04