I'm trying to return a dynamic object deserialized from a json string. At runtime I don't know what the object looks like so I can't type it.
I've tried this:
var json = @"[{""clientNumber"":""3052394"",""accountStatus"":""Active""},{""clientNumber"":""1700630"",""accountStatus"":""Active""}]";
dynamic result = JsonConvert.DeserializeObject(json);
return Json(result, JsonRequestBehavior.AllowGet);
But the result comes out like this:
[[[[]],[[]]],[[[]],[[]]]]
I know I can do this:
var result = new{...};
But this won't work an I don't know what the object is looking like at runtime.
So the standard Controller.Json
method in an MVC controller plays strangely with dynamic types.
Just as you did the deserialization with JSON.NET, you'd be better off doing the serialization with JSON.NET too and returning the string output.
return Content(JsonConvert.SerializeObject(dynamicInstance), "application/json");
What about Dictionary<string,string>
?
var j = new Dictionary<string,string>();
j.Add("clientNumber","3052394");
j.Add("accountStatus","Active");
return Json(j, JsonRequestBehavior.AllowGet);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With