What happens is that I have a request, I have a Json but I need to capture the value, but I have to do it without using a Model, I have seen examples but they all handle models.
is it possible to deserialize without a model?
At the moment I am doing it like this:
Step 1:
I create the Json from PostMan
{
"CAMPO_EN_LA_TABLA": "valor que quiero capturar"
}
Step 2:
Create a Private model in the class, so you can serialize:
private class Parametros
{
[Key]
public int Empid { get; set; }
public string Codigo { get; set; }
[Column(TypeName = "sql_variant")]
public object Valor { get; set; }
}
Step 3:
I deserialize it like this in a Dictionary:
var listParametros = JsonConvert.DeserializeObject<Parametros>(valor.ToString());
Step 4:
And the last thing is to capture the value and save it in some parameter:
object ValorCapturado = listParametros.Valor;
Console.WriteLine("Este es el valor impreso: " + listParametros.Valor);
As you can see, I know how to deserialize, but in the requirements they ask me to do it without the Model, but I don't know how to map it like that
If you need to deserialize generically, you can use the
JObject
Parsing JSON Object using JObject.Parse
can be deserialized without an object
Using JSON.NET for dynamic JSON parsing
parse the title "Importing JSON with JObject.Parse() and JArray.Parse()" but it basically defines
al usar
dynamic
no tendrias problema en deserializar el json sin una claseIf you use the JObject you can access the nodes
Parse JSON object in C# with different value types
In short you could use the JObject or dynamic to deserialize without a class to map the json to
Try doing the following:
we get a dynamic object without using a model. I hope it helps you, Greetings!
Documentation
One way to do what you're looking for is to use the dynamic type and JsonConvert.DeserializeAnonymousType Method (String, T)
I leave you a dotnetfiddle of the working example