C#:反序列化JSON列表 [英] C#: Deserialize JSON List
问题描述
你好:我有反序列化的问题Json:
这是我的Json(String):
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"
}
]
]}
这是我的类:
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; }
}
我需要使用de JSON生成一个Asignaturas列表。我尝试使用
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);
但不起作用。
- 请帮助我。
- 我使用Newtonsoft.Json
(编辑)
添加类:
(edit) Adding Class:
public class rootAsignatura
{
public Asignatura raiz;
public List<Asignatura> listaAsignaturas;
}
并尝试:
rootAsignatura listaAsignaturas = new rootAsignatura();
listaAsignaturas = JsonConvert.DeserializeObject<rootAsignatura>(json);
这样继续工作。
推荐答案
首先,我要与网络服务的所有者联系,告诉他们他们提供的JSON无效。
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.
无论如何,你可以通过修剪掉
和}
在字符串的开头和结尾。然后你剩下一个数组,其第一项是 Asignatura
,第二个是 Asignatura
的数组。
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.
如果您只想要一个 List< Asignatura>
,最简单的方法可能是反序列化为 JArray
然后解析单个元素:
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);
这篇关于C#:反序列化JSON列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!