我正在尝试将返回字符串解码为数组...我不知道它是否是json格式 返回代码看起来像这样..我做错了什么?
[
[
["संकल्प", "resolution", "Saṅkalpa", ""]
],
[
["noun", ["प्रस्ताव", "संकल्प", "समाधान", "स्थिरता", "चित्त की दृढ़ता", "प्रण"],
[
["प्रस्ताव", ["offer", "proposal", "motion", "resolution", "proposition", "offering"], , 0.69811249],
["संकल्प", ["resolution", "resolve", "determination", "pledge", "solemn vow"], , 0.53526145],
["समाधान", ["solution", "resolution", "settlement", "key", "resolvent", "redress"], , 0.064934582],
["स्थिरता", ["stability", "fixture", "fastness", "durability", "serenity", "resolution"], , 4.8327973e-05],
["चित्त की दृढ़ता", ["resolution", "strong will"], , 4.7578716e-05],
["प्रण", ["pledge", "vow", "capitulation", "determination", "resolution"], , 4.7578716e-05]
]
]
], "en", , [
["संकल्प", [4], 1, 0, 999, 0, 1, 0]
],
[
["resolution", 4, [
["संकल्प", 999, 1, 0],
["प्रस्ताव", 0, 1, 0],
["समाधान", 0, 1, 0],
["संकल्प के", 0, 1, 0],
["संकल्प में", 0, 1, 0]
],
[
[0, 10]
], "resolution"
]
], , , [
["en"]
], 5
]
Php Code ..
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
ini_set ( 'display_errors', 1 );
error_reporting ( E_ALL );
//text
$text = 'resolution';
$text = trim ( $text );
//Language Code hi => Hindi
$tl = 'hi';
$data = file_get_contents ( "http://translate.google.com/translate_a/t?ie=UTF-8&oe=UTF-8&client=t&sl=en&tl=" . $tl . "&sc=1&text=" . $text );
echo '<pre>';
print_r ( $data );
echo '</pre> <hr/>';
echo '<pre>';
var_dump ( json_decode ( $data, true ) );
echo '</pre>';
?>
</body>
</html>
答案 0 :(得分:2)
输入中有一部分只有,,如] ,,, [
这些使您的数据不具有JSONable,您可以删除em或填充空白,如,“”,
使用http://jsonlint.com/来修复输入,非常有用
编辑,这是有效的:
[
[
[
"संकल्प",
"resolution",
"Saṅkalpa",
""
]
],
[
[
"noun",
[
"प्रस्ताव",
"संकल्प",
"समाधान",
"स्थिरता",
"चित्त की दृढ़ता",
"प्रण"
],
[
[
"प्रस्ताव",
[
"offer",
"proposal",
"motion",
"resolution",
"proposition",
"offering"
],
"",
0.69811249
],
[
"संकल्प",
[
"resolution",
"resolve",
"determination",
"pledge",
"solemn vow"
],
"",
0.53526145
],
[
"समाधान",
[
"solution",
"resolution",
"settlement",
"key",
"resolvent",
"redress"
],
"",
0.064934582
],
[
"स्थिरता",
[
"stability",
"fixture",
"fastness",
"durability",
"serenity",
"resolution"
],
"",
0.000048327973
],
[
"चित्त की दृढ़ता",
[
"resolution",
"strong will"
],
"",
0.000047578716
],
[
"प्रण",
[
"pledge",
"vow",
"capitulation",
"determination",
"resolution"
],
"",
0.000047578716
]
]
]
],
"en",
"",
[
[
"संकल्प",
[
4
],
1,
0,
999,
0,
1,
0
]
],
[
[
"resolution",
4,
[
[
"संकल्प",
999,
1,
0
],
[
"प्रस्ताव",
0,
1,
0
],
[
"समाधान",
0,
1,
0
],
[
"संकल्प के",
0,
1,
0
],
[
"संकल्प में",
0,
1,
0
]
],
[
[
0,
10
]
],
"resolution"
]
],
"",
"",
[
[
"en"
]
],
5
]
答案 1 :(得分:2)
输入字符串的问题是连续(忽略空白)逗号。在JSONLint中手动更正它们会产生有效的JSON。
一个非常简单(尽管粗略)的解决方案是用一个逗号简单地替换所有出现的多个连续逗号(即,,
或,,,
等)。 JSONLint然后将您的字符串验证为有效的JSON,json_decode()
将返回一个数组。
正则表达式替换应该可以解决这个问题:
$string = "[ 'json' ]";
$string = preg_replace('/,(\s*,)+/', ',', $string);
$arr = json_decode($string);
$arr
现在是一个表示JSON数据的数组(不是某些人可能会假设的对象)。