1

I am in a controller in .net and returning a string using json.net to serialize data. the serialize method returns a string, so I just do a return in the controller for that string directly.

It is being returned with "'s and \"'s and thus coming back as invalid JSON for some reason. What are some common "gotchas" associated with returning strings of JSON directly from a controller that might be problematic?

This is the call:

String s = JsonConvert.SerializeObject(myListOf<Object>, settings); 
return s;

yet it comes back as invalid JSON. I suspect a formatting problem. thanks!

user3520332
  • 221
  • 4
  • 10
  • 1
    Post some of the returned json. What is the general format of your jobject? – James McDonnell Apr 10 '14 at 21:29
  • "[{\"ID\":\"1\",\"FIELD1\":null,\"FIELD3\":\"hello World\"}, ... like this. I read that in our controller when you return it it's wrapped automatically with \'s (WCF or something does this) and that I should be returning my object directly, but I can't because I need json.net's json instead of the object. I need to bypass it somehow. I heard you can use a stream to do it but memorystream isn't working for me yet – user3520332 Apr 10 '14 at 21:35
  • Look at the answer posted here? http://stackoverflow.com/questions/4729811/json-net-unexpected-characters-when-serializing-my-entities-entity-framew – James McDonnell Apr 10 '14 at 21:47
  • Yeah it seems that webapi will wrap it with \'s – user3520332 Apr 10 '14 at 22:03
  • Are you using MVC, Web API, or WCF? – Brian Rogers Nov 03 '14 at 02:24

0 Answers0