带有unicode字符的json_decode产生不正确的数组

时间:2014-01-29 12:50:50

标签: php unicode json

我在包含unicode字符的JSON字符串上使用json_decode,但它没有返回所需的输出。我不确定它是否包含错误或我做错了。

$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));

输出:

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] => صيدلة ) )

正如您所看到的,这会产生格式不正确的数组,但我不确定原因。任何帮助表示赞赏。

由于

2 个答案:

答案 0 :(得分:2)

你确定你没有两次编码你的json吗?我认为那些双斜线给你带来了麻烦:

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

我认为应该是这样的:

$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"}]';

编辑:

上面json的解析结果给出了以下内容:

[
{
    "name":"mobi7",
    "content":"jotform test"
},
{
    "name":"city7",
    "content":"الإسماعيلية"
},
{
    "name":"sex44",
    "content":"ذكر"
},
{
    "name":"age7",
    "content":"26"
},
{
    "name":"edu7",
    "content":"صيدلة"
}
]

答案 1 :(得分:0)

我的代码可以在\\之前添加u0627