I'm having a hard time consuming a payback json and convert- I've tried it in object so I can write to the database, my variables always return "null", I've tried several ways and I always get stuck in this return. Here's part of the code:
1) Json Content:
{
"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 in 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
}
}
}