I have a JSON.NET JObject with data structured like this:
{
"foo" : {
"bar": "baz"
}
}
I'm trying to convert it to a 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. Cannot access child value on Newtonsoft.Json.Linq.JValue.
I have a workaround, in that I can iterate through all of the properties of the JObject, and parse them into a generic object like so:
JsonResult jsonResult = Json(new { key1 = value1, key2 = value2, ... });
However, this seems error prone and like an unnecessary non-generic way of solving this problem. Is there any way I can do this more efficiently, hopefully using some built in methods in JSON.NET or ASP.NET MVC?