从mysql检索到的json字符串的json_decode不起作用

时间:2009-10-28 18:14:44

标签: php mysql json

我有一个字符串json,它提供以下var转储:

"[[{"TransactionID":"00416","OrderID":"000926","TransactionOrderItemID":"001123","LastUpdate":"2009-10-28 13:03:31","CustomerID":"184","Company_name":"Test123","Invoiced":"0","SubItemsCount":"2","ProductID":"1","ProductTypesID":"1","ProductTypeName":"Phone","ProductName":"Phone Line (Home)","IncludePST":"1","BillType":"Monthly","BillingCycle":"Monthly","Status":"Active","CreationDate":"2009-10-28","ActivationStartDate":"2009-10-28","NextNotificationDate":"2009-10-27","OverWritePrice":"-1","PriceEconomic":"26.00","BasePrice":"26.0000","ProRate":"Yes","InvoicePrice":"3.35","ServicePeriod":" Pro-Rate: Oct-28-2009 - Oct-31-2009","EndDate":"2009-10-31"}]]"

当我尝试解码为:

json_decode( $json, true);

结果只是null。 $ json来自db。
请注意,我只显示完整数据的子集。 问题是,这可以从db变化。如果双引号是问题,如何施放它?

2 个答案:

答案 0 :(得分:2)

您可以致电json_last_error()以获取有关错误的详细信息。

例如这样:

json_decode($string);

switch(json_last_error())
{
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
}

答案 1 :(得分:0)

这可能是UTF-8字符的问题。需要处理json_decode手册页suggest that some conversion issues上的许多帖子。

尝试使用json_decode(utf8_encode($json));