0

I use php json_decode something from wikipedia, but something not display. I have added

header('Content-Type: text/html; charset=utf-8');

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

and

$data = json_decode(utf8_encode($body));

This miss thing is \/m\u0259\u02c8d\u0292\u028cskju\u02d0lz\/

1
  • Okay, I think I see the real issue now. You used utf8_encode where a _decode was the goal. Commented Feb 4, 2011 at 12:01

1 Answer 1

1

utf8_decode() does not look for string expressions like \u02c8. You have to decode it the other way round:

$data = json_decode($body, 1);  // first; converts \u1234 to strings

array_walk_recursive("utf8_decode_walk", $data);
function utf8_decode_walk($item, $key) {
    return utf8_decode($item);
}                               // replace UTF-8 with Latin-1

If it is a nested array, then you'll need array_walk_recursive with a wrapper function however.

Though if you send your output page with charset="UTF-8" anyway, you should not need the conversion step.

Sign up to request clarification or add additional context in comments.

5 Comments

@mario, Warning: array_map() [function.array-map]: Argument #2 should be an array in
@yuli chika: that would mean $data was invalid. Try to add a print json_last_error() in between to find out the reason.
@mateusza: Yes, indeed. @yuli While that could be changed, it's too deeply nested for using array_map() anyhow. Try using it without UTF-8 decoding, or otherwise array_walk_recursive.
@mario, only use $data = json_decode($body);,I can use foreach get the data, add your code, it show Call to undefined function json_last_error()
@yuli chika: It's available since recently only. So, which PHP version are you using? I'm not sure when support for \u1234 unicode escapes was added; maybe your version has problems with decoding them. Do you still see any \u02c8 when writing it out from within the foreach? (In that case ZendFrameworks Json_Service would be the better option to use.)

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.