解码 JSON 字符串解码返回空值


Decode JSON String decode returning null

我要把这个jsonString传递给我的查询

xxxxxx=createVenue&clientId=2&jsonString={"veneue":{"clientId":"b","name":"d","tagline":"f","phone":"b","address":"d","city":"f","state":"b","zip":"d","twitter":"f","license":"d","imagePath":"f","pickupLocation":"b"},"drinks":[{"type":"d","name":"f","servingTable":{"servingSize":"b","price":"d"}},{"type":"d","name":"f","servingTable":{"servingSize":"b","price":"d"}}],"spirits":[{"type":"d","name":"f","servingTable":{"servingSize":"b","price":"d"}},{"type":"d","name":"f","servingTable":{"servingSize":"b","price":"d"}}]}

但是当我用 php 解码它时,它会返回 null。

$clientId = trim($_REQUEST['clientId']);
        $jsonString = trim($_REQUEST['jsonString']);
        $decodedJSON = json_decode($jsonString);
        return $decodedJSON;

$decodedJSON 返回空值;

怎么了?

您的 json 似乎有效。
引号可能用反斜杠转义,因为您magic_quotes_gpc打开了。你需要做的是使用

$jsonString = trim(stripslashes ($_REQUEST['jsonString']));

在解析之前尝试输出像die(trim($_REQUEST['jsonString']));这样的值 - 你会看到字符串已损坏

所以在将此字符串插入 URL 的位置存在问题,在那个地方你应该做urlencode(json_encode(...))