简体   繁体   English

Google翻译API无法对JSON进行PHP解码

[英]PHP decode of JSON doesn't work from the Google translate API

I'm trying to decode this JSON returned by Google Translation Service: 我正在尝试解码Google翻译服务返回的JSON:

$r = '[[["Hola Mundo","Hello World",,,1]],,"en"]'

But when I do this: 但是当我这样做时:

echo json_decode($r);

it returns me null. 它返回我为null。

I got this manual maded for JS, just trying to made it with PHP 我得到了这本针对JS的手册,只是想用PHP来制作。

https://ctrlq.org/code/19909-google-translate-api https://ctrlq.org/code/19909-google-translate-api

To clarify your issue, 为了澄清您的问题,

JSON is a string (and in PHP should be quote encapsulated), in your code, you have not declared it as a string but an array, and just to be clear you can instantiate an array with a couple methods: JSON是一个字符串(在PHP中应该用引号封装),在您的代码中,您并未将其声明为字符串而是一个数组,为清楚起见,您可以使用以下两种方法实例化一个数组:

$myArray = array();
$myArray2 = [];

So to fix your issue; 因此,要解决您的问题; encapsulate your JSON string, replace echo json_decode($r) with var_dump($r) and voila 封装您的JSON字符串,用var_dump($r)echo json_decode($r)替换echo json_decode($r)

Eg 例如

$r = '[[["Hola Mundo","Hello World",,,1]],,"en"]';
var_dump(json_decode($r));

BUT that is not valid JSON syntax (check on http://jsonlint.com/ ) 不是有效的JSON语法(请检查http://jsonlint.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM