1

I'm trying to convert a json string containing utf-8 symbols to a php array.

$jsonString = '{"loginid" : "90", "username" : "\U0437\U0430\U043c\U043a\U0435"}';

$array = json_decode($jsonString,true);

Unfortunately json_decode returns null.

Where is my mistake?

3
  • That's not valid JSON. jsonlint.com Where is that coming from? Commented Feb 14, 2014 at 18:21
  • it's not even valid PHP either. $jsonString and $array, perhaps? Commented Feb 14, 2014 at 18:23
  • @deceze you are totally right... did not notice the missing \ Commented Feb 14, 2014 at 18:26

2 Answers 2

1

Use \u instead of \U. Try this:

$jsonString = '{"loginid" : "90", "username" : "\u0437\u0430\u043c\u043a\u0435"}';

$array = json_decode($jsonString,true);
Sign up to request clarification or add additional context in comments.

3 Comments

yeah i kind of rushed to post without reviewing what i was typing. fixed that. You are also right in your suggestion thet the u should be lowercase. As I commented in the first answer the Web Service I'Am getting this from is not delivering correct json.
I see. You just have to filter it first: $jsonString = preg_replace('/\\\U/','\u', $jsonString);
Yep this is the way to go. was about to reply that. it is more of a hack though. better fix this on the web service side. ty for your help.
1

Try escaping your slashes.

<?php

$jsonString = '{"loginid" : "90", "username" : "\\\U0430"}';

$array = json_decode($jsonString, true);

print "<PRE><FONT COLOR=ORANGE>"; print_r($array); print "</FONT></PRE>";

2 Comments

Yeah kind of to obvious... d'oh. So the WebService providing this is buggy. TY
bprayudha was right with the lowercase u. anyway thx for your help :)

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.