1

I'm trying to decode return string to array... i dont know whether its a json format or not return code look like this.. what i doing wrong?

[
    [
        ["संकल्प", "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>
10
  • 1
    It's somewhat of JSON-ey, but not really. What does the output of var_dump give you? Where is this data coming from? Commented May 4, 2013 at 8:13
  • @Bojangles NULL value... . Commented May 4, 2013 at 8:15
  • 1
    If it should be json, it isn't valid. json_decode() will return NULL. From which language are this characters? Looks very nice! :) Commented May 4, 2013 at 8:16
  • @hek2mgl its.. Indian national Language -> HINDI Commented May 4, 2013 at 8:17
  • Nope not JSON..ask Google :D Commented May 4, 2013 at 8:18

2 Answers 2

2

The problem with your input string is that there are consecutive (ignoring whitespace) commas. Manually correcting them in JSONLint yields valid JSON.

A very simple (albeit crude) solution is to simply replace all occurrences of more than one consecutive comma (i.e. ,, or ,,,, etc) with a single comma. JSONLint then validates your string as valid JSON, and json_decode() returns an array, as it should.

A regex replace should do the trick:

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

$arr = json_decode($string);

$arr is now an array (not an object as some may assume) representing your JSON data.

Sign up to request clarification or add additional context in comments.

Comments

2

There are some part in the input with only , , like ], , , [

These are making your data non-JSONable, you could strip em or fill with blank like ,"",

Use http://jsonlint.com/ to fix the input, is very useful

EDIT, this is valid:

[
    [
        [
            "संकल्प",
            "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
]

Comments

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.