mesnicka
mesnicka

Reputation: 2548

javascript JSON.parse can't parse \t from PHP json_encode

This is my array to be encoded in PHP:

array (size=1)
  0 => 
    object(stdClass)[70]
      public 'company' => string 'Mc.SUP s. r. o.' (length=15)
      public 'fullname' => string '     Jaroslav Antoň' (length=17)
      public 'id' => string '1956' (length=4)
      public 'editable' => string '1' (length=1)

The string result is:

'[{"company":"Mc.SUP s. r. o.","fullname":" \tJaroslav Anto\u0148","id":"1956","editable":"1"}]'

When trying to use Javascript JSON.parse I got this error message: SyntaxError: JSON.parse: bad control character in string literal at line 1 column 56 of the JSON data

I investigated it and the problematic is \t.

Does somebody know the easy way how to extra escape it? Something like options in json_encode function. Thanks in advance

Upvotes: 0

Views: 1450

Answers (1)

totas
totas

Reputation: 10760

The \t is an escaped tab character do you really need that character for the fullname field? You should maybe remove the tab character before doing json_encode as described here: https://stackoverflow.com/a/17176793/2030937

Upvotes: 1

Related Questions