How do I go through a response from json in c #?

0

I have a REST API that sends the following answer in json format and I want to go through it in order to add a listview to what I go through.

{
"resultado": "OK",
"datos": [
{
  "Pago": "1500.00",
  "Fecha": "2018-09-27"
},
{
  "Pago": "900.00",
  "Fecha": "2018-10-16"
}
]
}  

The server's response deserializo asi:

dynamic msjdes = JsonConvert.DeserializeObject(responseText);  

Where msjdes is the json format already in c # and it appears as I put it up.
How do I go through it to add them as Item to a listview?
Thank you very much ...

    
asked by Manny 30.10.2018 в 16:47
source

1 answer

0

You have to deserialize a class

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string json = "{\r\n\"resultado\": \"OK\",\r\n\"datos\": [\r\n{\r\n  \"Pago\": \"1500.00\",\r\n  \"Fecha\": \"2018-09-27\"\r\n},\r\n{\r\n  \"Pago\": \"900.00\",\r\n  \"Fecha\": \"2018-10-16\"\r\n}\r\n]\r\n}";

        var result = JsonConvert.DeserializeObject<RootObject>(json);

        foreach(var item in  result.datos){
            Console.WriteLine(item.Pago);
        }


    }
}

public class Dato
{
    public string Pago { get; set; }
    public string Fecha { get; set; }
}

public class RootObject
{
    public string resultado { get; set; }
    public List<Dato> datos { get; set; }
}   

As you will see when typing the deserialization you have access to the properties you could also do with the dynamic, only you will not have intellisense in the code

    
answered by 30.10.2018 / 17:02
source