Consume Json and direct to a database using C#

Asked

Viewed 179 times

2

I’m having trouble consuming a return and convert it into object to then write in the database, my variables always return "null", I have tried several forms and always stay barred in this return. Follows part of the code:

1) Content of Json:

{  
  "list":[  
    {  
      "Pedido":"1234",
      "Data":"2018-05-21",
      "Cliente":"Jose Teste",
      "Itens":[  
        {  
          "Codigo":"1",
          "Quantidade":1,
          "ItemNome":"Tomada trifasica Elgin",
          "Preco":25.50
        }
      ],
      "Total":25.50,
      "FormaDePagamento":"cheque",
    }
  ]
}

2) Model class C#:

namespace ApiTeste.Models
{
    public class Pedidos
    {
        public Pedido[] list { get; set; }
        //public List<Pedido> pedido { get; set; }
    }

    public class Pedido
    {
        public string NumPedido { get; set; }
        public string Data { get; set; }
        public string Cliente { get; set; }
        public Iten[] Itens { get; set; }
        public float Total { get; set; }
        public string FormaDePagamento { get; set; }
    }

    public class Iten
    {
        public string Codigo { get; set; }
        public int Quantidade { get; set; }
        public string ItemNome { get; set; }
        public float Preco { get; set; }
    }
}

3) Part of the method that returns Json to direct to the object:

using (HttpWebResponse retornoServJson = (HttpWebResponse)requisicaoWeb.GetResponse())
{
    using (Stream retornoServJson = retornoJson.GetResponseStream())
    {                   
        using (StreamReader retornoReaderJson = new StreamReader(retornoServJson))
        {
            var response = retornoReader.ReadToEnd();
            Pedido pedido = new JavaScriptSerializer().Deserialize<Pedido>(response);  //aqui o objeto retorna null
            string teste = pedido.NumPedido; //aqui o objeto retorna null
            string teste2 = pedido.Data; //aqui o objeto retorna null
        }
    }
}

2 answers

1

The Class Requested the Attribute NumPedido must be Pedido.

namespace ApiTeste.Models
{
public class Pedidos
{
    public Pedido[] list { get; set; }
    //public List<Pedido> pedido { get; set; }
}

public class Pedido
{
    public string NumPedido { get; set; }
    public string Data { get; set; }
    public string Cliente { get; set; }
    public Iten[] Itens { get; set; }
    public float Total { get; set; }
    public string FormaDePagamento { get; set; }
}

public class Iten
{
    public string Codigo { get; set; }
    public int Quantidade { get; set; }
    public string ItemNome { get; set; }
    public float Preco { get; set; }
}
}

About converting Json to the Requested Object.

Would use the Newtonsoft

using (HttpWebResponse retornoServJson = (HttpWebResponse)requisicaoWeb.GetResponse())
{
    using (Stream retornoServJson = retornoJson.GetResponseStream())
    {                   
        using (StreamReader retornoReaderJson = new StreamReader(retornoServJson))
        {
            var response = retornoReader.ReadToEnd();
            Pedido pedido = JsonConvert.DeserializeObject<Pedido>(response);
            string teste = pedido.NumPedido; //aqui o objeto retorna null
            string teste2 = pedido.Data; //aqui o objeto retorna null
        }
    }
}

The response must be String.

  • Elivelton, thanks for posting, but keeps returning "null", when I run in debug mode I see that Steps do not pass the class "Request", I believe that my implementation is wrong before this step, I will review. If you have a tip thank you. Hugs.

0

I spent a few days testing various solutions and arrived at this solution, so I share:

using (StreamReader retornoReaderJson = new StreamReader(retornoServJson))
{
    Global.retorno = (retornoReaderJson.ReadToEnd());

    string JSON = @Global.retorno;
    JObject pedido = JObject.Parse(JSON);

    IList<JToken> equipamentos = pedido["list"].Children().ToList();
    foreach (JToken equip in equipamentos)
    {
        IList<JToken> comandos = equip.Children().ToList();
        foreach (JToken cmd in comandos)
        {
            //Fica em looping passando dadopor dado
            dadoDaPedido = (cmd.ToString());
        }
    }
}

Browser other questions tagged

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