jeudi 10 novembre 2016

Convert JObject to anonymous object

I am trying to pass an object into a web api application. While serializing the object it converts it to a json string. But on the wep api application side, it gets the object parameter as a JObject. This code block is from the web api application;

//Read web api body content into a string variable
var resultStr = Request.Content.ReadAsStringAsync().Result;
//Convert json string to object with Newtonsoft
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<object>(resultStr);

This code generates a JObject but I need an anonymous object. Web api project doesn't knows the types. Any object types can be received.

I need an object like this.

object anonyObj = new { Prop1 = "Lorem" };

ExpandoObject type doesn't meet my requirements so converting to a dynamic type is not a solution for me.





Aucun commentaire:

Enregistrer un commentaire