I have a string json that gives the following var dump:
"[[{"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开发者_JAVA百科 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"}]]"
When I try to decode as:
json_decode( $json, true);
The result is just null. $json is from db.
Note that I am only showing a subset of full data. The questions is, this is variable from db. If double quote is the problem, how to cast it?You can call json_last_error()
to get more info about what went wrong.
For example this way:
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;
}
It could be a problem with UTF-8 characters. Many posts on the json_decode manual page suggest that some conversion issues need to be handled.
Try using json_decode(utf8_encode($json));
精彩评论