简体   繁体   中英

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.

  • Please Help me with this.
  • I'm using Newtonsoft.Json

(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.

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

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 Asignatura s.

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);

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