I have a JObject JSON.NET with data structured as follows:
{ "foo" : { "bar": "baz" } }
I am trying to convert it to ASP.NET MVC JsonResult as follows:
JObject someData = ...; JsonResult jsonResult = Json(someData, "application/json", JsonRequestBehavior.AllowGet);
When I do this, I get the following exception:
InvalidOperationException was unhandled by user code. Unable to access value for child Newtonsoft.Json.Linq.JValue.
I have a workaround in which I can iterate over all the properties of a JObject and parse them into a common object, for example:
JsonResult jsonResult = Json(new { key1 = value1, key2 = value2, ... });
However, this seems error prone and as an unnecessary non-general way to solve this problem. Is there a way to do this more efficiently, hopefully using some of the built-in methods in JSON.NET or ASP.NET MVC?
Raul agrait
source share