user2825384
user2825384

Reputation: 1

json_decode with unicode characters producing incorrect array

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

Upvotes: 0

Views: 370

Answers (2)

Slavenko Miljic
Slavenko Miljic

Reputation: 3856

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":"صيدلة"
}
]

Upvotes: 2

John Lucabech
John Lucabech

Reputation: 156

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

Upvotes: 0

Related Questions