Kevin F
Kevin F

Reputation: 169

C#: Deserialize JSON List

Hi: I have problems with deserialization Json: This is my Json(String):

{[
  {
    "IdGuid": "fac5d174-17d4-4330-a65e-07133e88e0ca",
    "Nombre": "Asignaturas",
    "Subtitulo": "Subtitulo de asignaturas",
    "Descripcion": "Descripcion de asignaturas",
    "urlFoto": "egio1.jpg"
  },
  [
    {
      "IdGuid": "a9a59e49-c318-4868-93a9-57347b4c4cad",
      "Nombre": "Ciencias Naturales",
      "Subtitulo": "",
      "Descripcion": "Ciencias",
      "urlFoto": "80.jpg"
    },
    {
      "IdGuid": "8ae0dc90-aa6a-4457-8e64-5f591f75416c",
      "Nombre": "Documentos",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "asd.jpg"
    },
    {
      "IdGuid": "2ffbe004-316d-4a82-b4fe-0c43169766ad",
      "Nombre": "Inglés",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "http://pue.jpg"
    },
    {
      "IdGuid": "62151f5c-f503-48a6-9801-c27e92aa240a",
      "Nombre": "Matemática",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "http://pue.jpg"
    }
  ]
]}  

and this is my class:

public class Asignatura
    {
        public String idGuid { get; set; }

        public String nombre { get; set; }

        public String subtitulo { get; set; }

        public String descripcion { get; set; }

        public String urlFoto { get; set; }
    }

And I need generate a List of Asignaturas with de JSON. I'm trying with

List<Asignatura> listaAsignaturas = new List<Asignatura>();
listaAsignaturas= JsonConvert.DeserializeObject<List<Asignatura>>(json);

but don't work.

(edit) Adding Class:

public class rootAsignatura
{
    public Asignatura raiz;
    public List<Asignatura> listaAsignaturas;
}

and trying:

rootAsignatura listaAsignaturas = new rootAsignatura();
listaAsignaturas = JsonConvert.DeserializeObject<rootAsignatura>(json);

This continue without work.

Upvotes: 1

Views: 6370

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

First, I'd contact the owner of the webservice and tell them they're serving invalid JSON.

The outer {...} that's there currently means that they're serving an Object, but that object has no keys and values (which are required), it just wraps an Array.

Anyway, you could get around this by trimming off the { and } at the beginning and end of the string. Then you're left with an Array whose first item is an Asignatura and the second is an Array of Asignaturas.

If you just want one List<Asignatura>, the easiest way is probably to deserialize into a JArray and then parse the individual elements:

/* Parse the underlying array after removing the opening and closing braces */
var array = JArray.Parse(json.Trim('{', '}'));

/* Deserialize the first item in the array */  
var signatureOne = array[0].ToObject<Asignatura>();

/* Deserialize the second item in the array as a List<Asignatura> */
List<Asignatura> signatureList = array[1].ToObject<List<Asignatura>>();

/* Add the first item to the array. You could also use Insert(0, signatureOne) to 
 * put it at the front. */
signatureList.Add(signatureOne);

Upvotes: 0

Matt Clark
Matt Clark

Reputation: 1171

Your JSON String has 2 arrays one array with one "Asignatura"which has another nested array of "Asignatura". Remove the second set of "[]" brackets.

Upvotes: 2

Related Questions