3
I can do the POST of this information:
{
"Nritem": 1,
"Cdprevenda": 3,
"Cdproduto": 7,
"Decomplitem": "",
"Descricao": "Depilação",
"Dtcadastro": "2015-11-27T13:53:35.120Z",
"Flsituacao": 1,
"Md5": "",
"Qtproduto": 18,
"Totalizador": "01T1700",
"Unidade": "UN",
"Vltabela": 50,
"Vlunitario": 50,
}
But if you put yourself between []
appears this message:
"itemprevenda":[
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'PreVendaWebAPI.Models.Itemprevenda' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."]
API that makes the POST:
// POST: api/Itemprevendas
[ResponseType(typeof(Itemprevenda))]
public async Task<IHttpActionResult> PostItemprevenda(Itemprevenda itemprevenda)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Implementar o Cditemprevenda e o Nritem automaticamente
decimal cd = await GetMaiorCod();
decimal nr = await GetMaiorNr(itemprevenda.Cdprevenda);
itemprevenda.Cditemprevenda = cd;
itemprevenda.Nritem = nr;
// *******************************************************
db.Itemprevenda.Add(itemprevenda);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (ItemprevendaExists(itemprevenda.Cditemprevenda))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = itemprevenda.Cditemprevenda }, itemprevenda);
}
Committed:
public partial class Itemprevenda
{
public decimal Cditemprevenda { get; set; }
public decimal Cdprevenda { get; set; }
public decimal Cdproduto { get; set; }
public decimal Nritem { get; set; }
public decimal Qtproduto { get; set; }
public decimal Vltabela { get; set; }
public decimal Vldesconto { get; set; }
public decimal Vlacrescimo { get; set; }
public decimal Vlunitario { get; set; }
public int Flsituacao { get; set; }
public System.DateTime Dtcadastro { get; set; }
public string Descricao { get; set; }
public string Unidade { get; set; }
public string Totalizador { get; set; }
public byte[] Md5 { get; set; }
public string Decomplitem { get; set; }
public decimal Cdvendedor { get; set; }
public decimal Vltotal { get; set; }
}
How do I deserialize this JSON?
myType would be the kind I’m going through to deserialize: Itemprevenda? And stringJson is the format that will be deserialized?
– leopiazzoli
Exactly, the myType would be the Itemprevenda. stringJson is the string that contains the json that Oce wants to deserialize.
– Andre.Santarosa
But you don’t have a string with JSON. What I have is Itemprevenda:
PostItemprevenda(Itemprevenda itemprevenda)
– leopiazzoli
So Voce wants to serialize the data? What exactly does Voce need?
– Andre.Santarosa
I am passing data in JSON to the API and need to decentralize to do the POST object by object.
– leopiazzoli
But isn’t your object already "deserialized"? Explain to me more the flow of your logic (step by step) and the final goal of the whole process, please...
– Andre.Santarosa
Let’s go continue this discussion in chat.
– Andre.Santarosa