Json request encoding issues

I am trying to manually create a Json string to send to the client.

{'result':'hhh'} 

When i use

echo json_encode(array('result'=>'hhh'));

He arrives perfectly. But when I do

echo "{'result':'hhh'}";

Is not

The only difference that I find between the two queries is that the first one has:

Content-Length: 9    header

and the second (which does not work)

Content-Length: 16   header

Both lines should contain the content length: 16 !!! I assume this is due to a combination of ZF and Mootools.

+3
source share
2 answers

According to specs, JSON requires double quotes around key names and string values.

echo json_encode(array('result'=>'hhh'));

displays

{"result":"hhh"}

The length of this pin is 16 bytes, as shown below:

echo strlen(json_encode(array('result'=>'hhh')));

displays "16".

JSON-, , , JSON - .

+2

UTF-8, UTF-8. , , , json_encode() .

+1

Source: https://habr.com/ru/post/1705378/


All Articles