0

I am using json_decode on a JSON string containing unicode characters but it is not returning the desired output. I'm not sure if it is the string which contains errors or I am doing something wrong.

$test = '[{"name":"mobi7","content":"jotform test"},{"name":"city7","content":"\\u0627\\u0644\\u0625\\u0633\\u0645\\u0627\\u0639\\u064a\\u0644\\u064a\\u0629"},{"name":"sex44","content":"\\u0630\\u0643\\u0631"},{"name":"age7","content":"26"},{"name":"edu7","content":"\\u0635\\u064a\\u062f\\u0644\\u0629"}]';

print_r(json_decode($test, true));

This outputs:

Array ( [0] => Array ( [name] => mobi7 [content] => jotform test ) [1] => Array ( [name] => city7 [content] => u0627لإسماعيلية ) [2] => Array ( [name] => sex44 [content] => ذكر ) [3] => Array ( [name] => age7 [content] => 26 ) [4] => Array ( [name] => edu7 [content] => صيدلة ) )

As you can see this produces an incorrectly formatted array but I am not sure why. Any help is appreciated.

Thanks

2 Answers 2

2

Are you sure you're not encoding your json twice? I think those double slashes are giving you trouble:

\\u0635\\u064a\\u062f\\u0644\\u0629

I think it should look like:

$test = '[{"name":"mobi7","content":"jotform test"},{"name":"city7","content":"\u0627\u0644\u0625\u0633\u0645\u0627\u0639\u064a\u0644\u064a\u0629"},{"name":"sex44","content":"\u0630\u0643\u0631"},{"name":"age7","content":"26"},{"name":"edu7","content":"\u0635\u064a\u062f\u0644\u0629"}]';

EDIT:

Parsed result of the json above gives me the following:

[
{
    "name":"mobi7",
    "content":"jotform test"
},
{
    "name":"city7",
    "content":"الإسماعيلية"
},
{
    "name":"sex44",
    "content":"ذكر"
},
{
    "name":"age7",
    "content":"26"
},
{
    "name":"edu7",
    "content":"صيدلة"
}
]
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think that is the problem here. It produces the same result with or without the double slashes
Are you sure? I've just tested it on: json.parser.online.fr where I get some arabic characters when parsed
The website parser you linked to also produces the correct result for the double slash but running it in php returns incorrectly formatted array
OSX Chrome 32.0.1700.77 I only get proper results when using single slashes.
0

Your code works ok for me adding \\ before u0627.

1 Comment

oh yeah sorry. I removed that for testing and forgot to add back in. It still doesn't work for me though even with the slashes added

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.