Creating a List from a Json C #

8

I have a question 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
}

I would like to know how to get the list attribute and convert it to a List

asked by anonymous 20.04.2017 / 22:39

3 answers

5

Make the following classes to get the same layout of this 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 this:

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

The information in the is loaded for this class Base .

20.04.2017 / 22:54
11

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

  

Edit > Past Special > Past JSON As Class

Classgeneratedbyvisualstudio

publicclassBase{publicstringjsonrpc{get;set;}publicResultresult{get;set;}publicintid{get;set;}}publicclassResult{publicintnr{get;set;}publicLista[]lista{get;set;}}publicclassLista{publicstringcodigo{get;set;}publicstringnome{get;set;}publicstringfoto{get;set;}}

Thenwithyourclasscreatedautomaticallybyvisualstudio,youconverttotheobjectusingtheNewtonSoft.Jsonlibrary

stringvalue=File.ReadAllText("arq.json");
Base b = Newtonsoft.Json.JsonConvert.DeserializeObject<Base>(value);
    
20.04.2017 / 23:07
0

Or, you can use a json converter for object c # like this, for example: link

And also you would not 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))
    
20.04.2017 / 23:00