2013-05-04 77 views
1

我想解码返回字符串数组...我不知道它是否是一个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代码..

<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> 
+1

这有些JSON-ey,但不是真的。 'var_dump'的输出给你什么?这些数据来自哪里? – Bojangles 2013-05-04 08:13:50

+0

@Bojangles NULL值...。 – 2013-05-04 08:15:00

+1

如果它应该是json,则无效。 'json_decode()'将返回NULL。这些字符来自哪种语言?看起来很好! :) – hek2mgl 2013-05-04 08:16:05

回答

2

与你输入字符串的问题是,有连续的(忽略空格)逗号。在JSONLint中手动更正它们会生成有效的JSON。

一个非常简单的(尽管粗糙的)解决方案是用一个逗号简单地替换所有出现的多个连续的逗号(即,,,,,等)。 JSONLint然后验证您的字符串为有效的JSON,并且json_decode()返回一个数组,就像它应该那样。

甲正则表达式替换应该做的伎俩:

$string = "[ 'json' ]"; 
$string = preg_replace('/,(\s*,)+/', ',', $string); 

$arr = json_decode($string); 

$arr现在是表示您的JSON数据阵列(未一些可以假定的对象)。

+0

Yup其工作.. – 2013-05-04 08:25:43

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 
]