8

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.

3
  • In my opinion, there is something wrong in your json. There are too many commas Commented Jan 28, 2016 at 14:05
  • When you try result.ToString() you should see your json, if not your json is not proper. Commented Jan 28, 2016 at 14:06
  • 4
    You get a json payload, deserialize it and then serialize it back to return it? Why don't you jus treturn the original json payload then? Commented Jan 28, 2016 at 14:06

4 Answers 4

10

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Except that it doesn't make sense to deserialize json and then in the next line serialize it back. He should just return the original json payload then...
@dustinmoris Yes, that's a given. I'm left with the feeling that there's more to the OPs code than that which he has chosen to show us, otherwise we're looking at the most pointless programming exercise I've seen in a while.
If Microsoft.AspNet.WebApi.Core used, JsonResult is same internally serializes objects by json.net
5

What about Dictionary<string,string> ?

var j = new Dictionary<string,string>();
j.Add("clientNumber","3052394");
j.Add("accountStatus","Active");
return Json(j, JsonRequestBehavior.AllowGet);

1 Comment

this is not dynamic object
0

This is because JsonConvert.DeserializeObject(json) return JArray instance.

And (i dont know why) not serializes correctly in return Json() of ApiController

I my case only helps to define static type and serialize to it before return

Comments

-2

This isn't a valid JSON because you have to much " in your JSONString. I don't know if this solves your problem, but try this one:

var json = @"[{\"clientNumber\":\"3052394\",\"accountStatus\":\"Active\"},{\"clientNumber\":\"1700630\",\"accountStatus\":\"Active\"}]";

2 Comments

JsonConvert.DeserializeObject() passed, this means json is ok
if using @"" you escape quotes using "" and not \"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.