简体   繁体   中英

Iterate Json in C#

I have a problem now, I can iterate this JSON with the following code, but I'm left with no values for "libros" like "titulo" "author" etc. what's wrong with me? or how can I get the "Libros" list to iterate correctly. i using RestSharp and

   namespace ConsoleApp1
   {
    public class Libro
    {
    public int id { get; set; }
    public string id_categoria { get; set; }
    public string tipo { get; set; }
    public string titulo { get; set; }
    public string ruta { get; set; }
    public string autor { get; set; }
    public int estado { get; set; }
    public string titular { get; set; }
    public int size { get; set; }
    public string detalles { get; set; }
    public double precio { get; set; }
    public string portada { get; set; }
    public int ventas { get; set; }
    public int oferta { get; set; }
    public double precioOferta { get; set; }
    public int entrega { get; set; }
    public string fecha { get; set; }
}

public class Cate
{
    public int id { get; set; }
    public string categoria { get; set; }
    public string ruta { get; set; }
    public int estado { get; set; }
    public int oferta { get; set; }
    public int precioOferta { get; set; }
    public int descuentoOferta { get; set; }
    public string imgOferta { get; set; }
    public string finOferta { get; set; }
    public string fecha { get; set; }
    public IList<Libro> libros { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var client = new RestClient("wwwwwwwww");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response);
        Console.WriteLine("mostrar registros");
        Libro[] resultados = JsonConvert.DeserializeObject<Libro[]>(response.Content);

        foreach (var resultado in resultados)
        {
            Console.WriteLine(resultado.portada);
        }
        Console.WriteLine("terminado...");
        Console.ReadKey();


    }
}
}

for now I can't modify the json generation, but I also want to know if it's possible that the json is wrong

This is the JSON

        [
{
id: 1,
categoria: "CIENCIA-FICCION",
ruta: "ciencia-ficcion",
estado: 1,
oferta: 0,
precioOferta: 0,
descuentoOferta: 0,
imgOferta: "",
finOferta: "0000-00-00 00:00:00",
fecha: "2020-01-13 14:43:36",
libros: [
{
id: 11,
id_categoria: "6,5,1",
tipo: "",
ruta: "fundamentos",
autor: "J.DM",
estado: 1,
titulo: "fundamentos",
titular: "Titular",
size: 166872,
detalles: "",
precio: 24,
portada: "vistas/img/cabeceras/fundamentos.jpg",
ventas: 0,
oferta: 0,
precioOferta: 0,
entrega: 0,
fecha: "2020-04-01 14:35:59"
}
]
},
{
id: 2,
categoria: "FANTASIA",
ruta: "fantasia",
estado: 1,
oferta: 0,
precioOferta: 0,
descuentoOferta: 0,
imgOferta: "",
finOferta: "0000-00-00 00:00:00",
fecha: "2020-01-13 14:43:58",
libros: [
{
id: 15,
id_categoria: "5,2",
tipo: "",
ruta: "el-principito",
autor: "ANTOINE DE SAINT - EXUPÉRY ",
estado: 1,
titulo: "El principito",
titular: "Titular",
size: 109504,
detalles: "",
precio: 12,
portada: "vistas/img/cabeceras/el-principito.jpg",
ventas: 0,
oferta: 0,
precioOferta: 0,
entrega: 0,
fecha: "2020-03-02 13:57:03"
},
{
id: 16,
id_categoria: "5,2",
tipo: "",
ruta: "el-asno-y-el-caballo",
autor: "CRISTINA RODRÍGUEZ LOMBA",
estado: 1,
titulo: "El asno y el caballo",
titular: "Titular",
size: 8600,
detalles: "",
precio: 0,
portada: "vistas/img/cabeceras/el-asno-y-el-caballo.jpg",
ventas: 0,
oferta: 0,
precioOferta: 0,
entrega: 0,
fecha: "2020-04-01 13:57:03"
}
]
}
]

The problem is that you are looping/looking at the structure of the file/code in a wrong way.

your .json content is like

[
Cate:{
  Libros:{
  } 
}
]

As you have in your classes defined correctly, Libros are inside the Cate

Which means that you have Libros inside the category, But when you got the results, you try to get Libros[] , but you have to get Cate[] like the next:

Cate[] resultados = JsonConvert.DeserializeObject<Cate[]>(response.Content);

Then If you want to iterate the books, you need to loop first each category and then each book inside the category.

foreach (var categoria in resultados)
{
    foreach(var libro in categoria.libros)
    {
        Console.WriteLine(libro.portada);
    }
}

btw: portada and ruta values are swapped in the json file.

not related to the issue, but the names of the properties in the objects (id, categoria, ruta, estado, etc) should begin with uppercase.

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