Creating a List<> from a Json C#

Asked

Viewed 5,878 times

11

I have a doubt regarding JSON, I have the following JSON:

{
  "jsonrpc":"2.0",
  "result":{
    "nr":26,
    "lista":[
      {
        "codigo":"2",
        "nome":"Pratos Quentes",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"3",
        "nome":"Sobremesas",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"4",
        "nome":"Bebidas Nao Alcoolicas",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"7",
        "nome":"Cocktails",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"10",
        "nome":"Cafes",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"11",
        "nome":"Consummes",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"12",
        "nome":"Porções",
        "foto":"./images/cupcake.png"
      },
      {
        "codigo":"13",
        "nome":"Chocolates",
        "foto":"./images/cupcake.png"
      }
    ]
  },
  "id":138827
}

would like to know how I take the list attribute and convert it to a List<> or C object array#.

  • You intend to do everything "manually" or use a library that abstracts it?

  • I’m trying with Newtonsoft.Json but I can’t find an example in the documentation

3 answers

12

In visual studio you can transform any string in the pattern json in class C#, just follow the steps below:

Edit > Past Special > Past JSON As Class

inserir a descrição da imagem aqui


Class generated by visual studio

public class Base
{
    public string jsonrpc { get; set; }
    public Result result { get; set; }
    public int id { get; set; }
}

public class Result
{
    public int nr { get; set; }
    public Lista[] lista { get; set; }
}

public class Lista
{
    public string codigo { get; set; }
    public string nome { get; set; }
    public string foto { get; set; }
}

Then with your class automatically created by visual studio, you convert to the object using the Newtonsoft.Json

string value = File.ReadAllText("arq.json");
Base b = Newtonsoft.Json.JsonConvert.DeserializeObject<Base>(value);
  • 4

    James, sensational it from here. Thank you for learning!

6


Do the following classes to get the same layout that with the package Newtonsoft.Json - Json.NET decorating each property as follows:

public class Base
{
    [Newtonsoft.Json.JsonProperty("id")]
    public int Id { get; set; }

    [Newtonsoft.Json.JsonProperty("jsonrpc")]
    public string JsonRpc { get; set; }

    [Newtonsoft.Json.JsonProperty("result")]
    public Result Result { get; set; }
}

public class Result
{
    [Newtonsoft.Json.JsonProperty("nr")]
    public int Nr { get; set; }

    [Newtonsoft.Json.JsonProperty("lista")]
    public List<Items> Lista { get; set; }
}

public class Items
{

    [Newtonsoft.Json.JsonProperty("codigo")]
    public int Codigo { get; set; }

    [Newtonsoft.Json.JsonProperty("nome")]
    public string Nome { get; set; }

    [Newtonsoft.Json.JsonProperty("foto")]
    public string Foto { get; set; }
}

then use like this:

string value = File.ReadAllText("arq.json");
Base b = Newtonsoft.Json.JsonConvert.DeserializeObject<Base>(value);

ready so is loaded the information of the for this class Base.

References:

  • 1

    Thank you Virgílio for sharing knowledge!

0

Or, you can use a json to Object c# converter like this, for example: https://jsonutils.com/

And you also wouldn’t need to use a third-party package with: new System.Web.Script.Serialization

 var objConvert = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(stringJson, typeof(tipodoseuobjeto))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.