0

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

mesnicka
  • 2,458
  • 8
  • 23
  • 31
  • 2
    Do you need those `\t`'s in the values? Why dont you recursively strip all leading and trailing white spaces in the array? – enemetch Aug 02 '17 at 12:55
  • Possible duplicate of [PHP's json\_encode does not escape all JSON control characters](https://stackoverflow.com/questions/1048487/phps-json-encode-does-not-escape-all-json-control-characters) – Jared Smith Aug 02 '17 at 12:55

1 Answers1

1

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

totas
  • 10,288
  • 6
  • 35
  • 32