0

I have a JSON string that contains Dal\u00e9. When I use json_decode on the JSON, it is converted to Dalé, however the original string that the JSON is from is Dalé. Why is this not converted properly?

I have found that "\u00E9" is the C/C++/Java source code encoding for é. However, to me this doesn't answer why this is going wrong.


Example of incorrect PHP output:

<?php
$opts = array('http'=>array('ignore_errors' => true));
$context = stream_context_create($opts);
$jsonurl = "http://api.kivaws.org/v1/loans/552804.json";
$json = file_get_contents($jsonurl, false, $context);
$json_output = array(json_decode($json));
$json_error = $json_output[0]->error;
$json_message = $json_error->message;

foreach ($json_output[0]->{'loans'} as $loan) {
echo 'Name: '.$loan->{'name'};
}
?>
18
  • Because PHP hates Unicode. Commented Jun 24, 2013 at 4:56
  • 3
    php handles unicode escape sequences in json perfectly fine. Likely, you have borked the string somewhere else, or you haven't told the web browser that you're giving it utf8. Commented Jun 24, 2013 at 4:58
  • ideone.com/0BRsYA - it works fine Commented Jun 24, 2013 at 5:00
  • 1
    @Ignacio Vazquez-Abrams: you're overdramatizing Commented Jun 24, 2013 at 5:01
  • Perhaps. But that doesn't necessarily mean that I'm wrong. Commented Jun 24, 2013 at 5:01

2 Answers 2

3

You need to tell the web browser what encoding you are giving it.

<?php
header('content-type: text/plain; charset=utf-8');
var_dump(json_decode($jsonStr));
Sign up to request clarification or add additional context in comments.

6 Comments

Note: for my case (having this inside a webpage) I will be using header('content-type: text/html; charset=utf-8');
@michaellindahl: your comments are confusing ;-)
@zerkms they make complete sense to me :P apparently I wasn't setting the charset and relying on the default which was wrong. I didn't understand this needed to be set in the PHP header, which I've never had to do before.
Any reason why this needs to be done? Should I be setting the header in the PHP for every webpage or just in certain circumstances?
@michaellindahl otherwise, a browser has to guess. And, there's many situations where it's not possible to guarantee a correct guess, which leads to erratic and baffling bugs throughout your software. A robust piece of web software always specifies the encoding, for every page, every time.
|
1

if you are using php 5.4 you may use the function options of json_encode() like this :-

echo $b=json_encode('Dalé',JSON_UNESCAPED_UNICODE);
echo json_decode($b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.