Parsing a Json with NewtonsJson

motkingmotking Member

Hi! I'm new in Xamarin and C# and I'm trying to read the following json:

{"message":"OK","status":true,"data":[{"codigo":"1","nombre":"Jose Perez","edad":"32","correo":"[email protected]","pas":"123456"},{"codigo":"2","nombre":"Carlos Hernandez","edad":"32","correo":"carlos.hernan[email protected]","pas":"654321"}]}

First, I need to validate if "message" value is "OK". Then, I need to get the info contained in "data". I was using NewtonsJson with a simple exercise and it was ok, but in this case I don't know how to read this json.

Thank you for all!

Answers

  • kiwibeenzkiwibeenz Member ✭✭

    http://json2csharp.com/
    You need a class to bind that info to (use the link above)..this should look like below
    public class Datum //rename class to suit
    {
    public string codigo { get; set; }
    public string nombre { get; set; }
    public string edad { get; set; }
    public string correo { get; set; }
    public string pas { get; set; }
    }

    public class RootObject //rename to suit
    {
    public string message { get; set; }
    public bool status { get; set; }
    public List data { get; set; }
    }
    You tube is good, also look out for a tool called POSTMAN download (not browser ext). You tube how to use it will change your life.

  • motkingmotking Member
    edited May 2018

    @kiwibeenz said:

    You need a class to bind that info to (use the link above)..this should look like below
    public class Datum //rename class to suit
    {
    public string codigo { get; set; }
    public string nombre { get; set; }
    public string edad { get; set; }
    public string correo { get; set; }
    public string pas { get; set; }
    }

    public class RootObject //rename to suit
    {
    public string message { get; set; }
    public bool status { get; set; }
    public List data { get; set; }
    }
    You tube is good, also look out for a tool called POSTMAN download (not browser ext). You tube how to use it will change your life.

    Thanks for your answer, I think I have the idea but still don't get it right. I created this class named User:

    {
    public class User
    {
    public int codigo { get; set; }
    public string nombre { get; set; }
    public int edad { get; set; }
    public string correo { get; set; }
    public string pas { get; set; }
    }

    public class WSResponse
    {
        public string message { get; set; }
        public bool status { get; set; }
        public List<User> data { get; set; }
    }
    

    }

    And in the method where I receive the json from the web service I have this:

    if (res.IsSuccessStatusCode)
    {
    string content = await res.Content.ReadAsStringAsync();
    List respuesta = JsonConvert.DeserializeObject<List>(content);
    }

    I don't know if this is the right way to do it, but I don't know yet how to pull out the value of message for example.
    Thank you for your suggestions about Postman, I will look about it later :smile:

  • kiwibeenzkiwibeenz Member ✭✭

    nice video to put it all in context

Sign In or Register to comment.