E
E
Eugene Vdovenko2015-09-15 12:03:31
PHP
Eugene Vdovenko, 2015-09-15 12:03:31

How to solve the problem of stripping slashes in json_encode?

There is an array like

array(
   "name"=>"Сулаймон",
   "custom"=> array(
        array(
          "name":"country",
          "content":"Узбекистан"
        ),
        array(
           "name":"last_date",
            "content":"2015-09-15"
        )
    )
)

after json_encode I get something like
{"name":"u043cu0438u0445u0430u0438u043b","custom":[{"name":"country","content":"u0422u0438u0440u0430u0441u043fu043eu043bu044c"},{"name":"last_date","content":"2015-09-15"}]}

Who-thread faced with this? How to decide?
PS: In addition to substituting the JSON_UNESCAPED_UNICODE flag. And I don’t really want to climb into third-party libraries.
PS:
Linux versions bla-blabla 3.2.0-4-amd64 #1 SMP Debian 3.2.63-2+deb7u1 x86_64
PHP Version 5.6.6-2
json version 1.3.6
JSON-C headers version 0.11.99
JSON-C library version 0.11.99

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Vdovenko, 2015-12-24
@Misanthropist

In general, this is how:
1. If there is a problem with encoding: the second parameter of the json_encode() function is the JSON_UNESCAPED_UNICODE flag. Then non-ANSI characters are not translated into codes. It is only necessary to ensure that all work with data is done in UTF-8.
2. There is a similar problem when working with PDO: if you use prepared queries and form a query with already inserted data, and not through bind(), then PDO filters the query text and removes slashes. If done as it should be for prepared queries - via bind() - then the slashes remain as they are.

M
Mikhail Osher, 2015-09-15
@miraage

I bet you have old PHP/libjson/etc.
Kubuntu 14.04
PHP 5.6.12-1+deb.sury.org~trusty+1 (cli)

$x = array(
    "name" => "Сулаймон",
    "custom" => array(
        array(
            "name" => "country",
            "content" => "Узбекистан",
        ),
        array(
            "name" => "last_date",
            "content" => "2015-09-15",
        ),
    ),
);

echo json_encode($x);

// {"name":"\u0421\u0443\u043b\u0430\u0439\u043c\u043e\u043d","custom":[{"name":"country","content":"\u0423\u0437\u0431\u0435\u043a\u0438\u0441\u0442\u0430\u043d"},{"name":"last_date","content":"2015-09-15"}]}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question