简体   繁体   English

C#:反序列化JSON列表

[英]C#: Deserialize JSON List

Hi: I have problems with deserialization Json: This is my Json(String): 嗨:我对反序列化有问题Json:这是我的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. 我需要使用de JSON生成Asignaturas列表。 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 我正在使用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". 您的JSON字符串有2个数组,一个数组带有一个“ Asignatura”,另一个带有“ Asignatura”的嵌套数组。 Remove the second set of "[]" brackets. 卸下第二组“ []”括号。

First, I'd contact the owner of the webservice and tell them they're serving invalid JSON. 首先,我将与Web服务的所有者联系,并告诉他们他们正在提供无效的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. 当前存在的外部{...}表示它们正在提供对象,但是该对象没有键和值(必需),它仅包装一个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. 然后你留下了一个数组,其第一个项目是Asignatura ,第二个是数组Asignatura秒。

If you just want one List<Asignatura> , the easiest way is probably to deserialize into a JArray and then parse the individual elements: 如果只需要一个List<Asignatura> ,则最简单的方法可能是反序列化为JArray然后解析各个元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM