0

I searched a lot in google and stackoverflow, but couldn't find a solution.

I've the following json coming from DB:

{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"}

As you can see, there are some utf8 chars that were not saved as unicode. And when I try to decode this json, it returns null and json_last_error returns 5.

Does anyone have a solution?

3
  • Error 5 is JSON_ERROR_UTF8. I think you need to format the character ª some other way (if at all possible). Commented Apr 4, 2014 at 12:34
  • Which version of php do you use? Commented Apr 4, 2014 at 12:58
  • I'm using PHP 5.3.3-7 Commented Apr 4, 2014 at 13:01

2 Answers 2

3

Use like following. Use with single quote;

$a = '{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"}';
var_dump(json_decode($a));

Here is working demo: codepad

If not work, alternatively you can use iconv to convert your stirng to utf-8 and decode like;

<?php
$a = '{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"}';
$a = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($a));
$json = json_decode($a);
var_dump($json);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

@RakeshSharma how? I have provided demo. Is not working demo for you?
Could you try updated code if you have iconv installed?
ignoring utf8 it worked too! thanks!!! I chose the other answer because was way simpler! :P
1

try

$utfstr= mb_convert_encoding($str ,"UTF-8");
$output = json_decode($utfstr, true);

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.