1

I have a problem that my JSON service is not being called, due to bad format probably.

Still, I dont understand what is wrong it it. I read about it and found out that apostrophes should not be escaped. Also when I escape them, it doesnt work.

"{
  "fields": [
   {
     "Text": "PaymentReminders",
     "Value": "'yes'"
   }
 ]
}"

And yes, I really need 'yes' to be under apostrophes.

I am expecting a String on server side, which I then deserialize. It works without apostrophes.

Thanks!

edit1:

This is the structure that accepts in on the server:

Public Class TemplateField
    Public Property Value() As String = "val"
    Public Property Text() As String = "tex"
End Class

Public Class FieldsList
    Public Property fields() As TemplateField()
End Class

and it gets deserialzed like this:

Dim jsSerializer As New JavaScriptSerializer

Dim fieldsArray As EventInfoDetails.FieldsList
fieldsArray = jsSerializer.Deserialize(Of EventInfoDetails.FieldsList)(fields)

and all that works, unless it contains apostrophes. Like I cannot stick apostrophe inside a string.

lucafik
  • 265
  • 1
  • 5
  • 17
  • http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json – Christophe Roussy Mar 24 '14 at 15:26
  • the problem is when i do it with `"` instead of `'`, I cannot deserialize it on the server side. It wont fit into a String type. – lucafik Mar 24 '14 at 15:46
  • There server should support it if the JSON is valid, can you change the server code ? – Christophe Roussy Mar 24 '14 at 15:52
  • what library are you using at the server end? are you able to paste the raw json string? is it exactly what you expect just before you pass it to the json lib or some code is escaping for u ... – tgkprog Mar 24 '14 at 18:42
  • I am using plain .NET, System.Web.Script.Services.JavaScriptSerializer class. I have edited my question and added relevant serverside stuff. – lucafik Mar 27 '14 at 12:56

1 Answers1

1

JSON does not only not require to escape apostrophes, but in fact it does not allow doing so (contrary to JavaScript). So your

"Value": "'yes'"

Is perfectly valid JSON. This is, unless you were inserting this JSON as a String literal inside JavaScript code, in which case it would be JavaScript the one requiring you to escape your ' as \' (you'd need two escapes, the JSON one and the JavaScript one on top of it).

Anyway, there's something strange about your code:

"{
  "fields": [
   {
     "Text": "PaymentReminders",
     "Value": "'yes'"
   }
 ]
}"

Why is your entire JSON structure surrounded by quotes (")? Is it a string literal of any kind inside other programming language? In such case, you might need to follow that language's escaping rules for those quote (") symbols. Both Java and VB, for example, would use \" there...

Daniel Fernández
  • 6,775
  • 2
  • 28
  • 32