7

I am in the process of converting some of our web "services" to MVC3 from WCF Rest.

Our old web services returned JSON from POCO's just fine using: [WebGet(.... ResponseFormat=WebMessageFormat.Json]

In my controller to return back a simple poco I'm using a JsonResult as the return type, and creating the json with Json(someObject, ...).

In the WCF Rest service, the apostrophes and special chars are formatted cleanly when presented to the client.

In the MVC3 controller, the apostrophes appear as \u0027.

Any thoughts? I'm new to serializing JSON so any pointers would be a huge help.

Example response: WCF Rest: {"CategoryId":8,"SomeId":6,"Name":"Richie's House"}

MVC3: {"CategoryId":8,"SomeId":6,"Name":"Richie\u0027s House"}

LukLed
  • 31,452
  • 17
  • 82
  • 107
Richard
  • 1,677
  • 3
  • 13
  • 15

3 Answers3

11

That shouldn't be any problem, as both representations are equivalent:

var a = {"CategoryId":8,"SomeId":6,"Name":"Richie\u0027s House"};
alert(a.Name);

alerts Richie's House.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 7
    Oh...my... gosh..... My browser addon on my desktop was just formatting it to make it readable... grrrr I need more coffee. Sorry to waste your time. – Richard Feb 16 '11 at 21:42
2

Just do:

yourObject.Name = yourObject.Name.replace("'", "\\u027");

So, if you try to alert in javascript or show in a browser, it will appears like:

Richie's House

Venrix
  • 473
  • 6
  • 16
Alexandre TRINDADE
  • 917
  • 10
  • 21
1

U+0027 is Unicode for apostrophe (')

So, special characters are returned in Unicode but will show up properly when rendered on the page.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56