Reputation: 3875
I Have this data from my API Call.
[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"'42060106''9405510200830072094975'\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]
How can I convert this data into PHP Array? I tried using json_decode($result,true)
but it is not working properly. Thanks in advance.
UPDATE:
My PHP Code
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
//echo $result;
Upvotes: 0
Views: 3004
Reputation: 26258
use json_decode()
for this:
// It will convert the given json to an array
$arr = json_decode($json, true);
// The second param is for array, o/w it will return an object
Upvotes: 4
Reputation: 2935
Try this,
$jsonData = stripslashes(html_entity_decode($result));
$output = json_decode($jsonData, true);
echo "<pre>";
print_r($output);
echo "</pre>";
Or Try this way,
$out = preg_replace('/\\\"/',"\"", $result);
$output = json_decode($out, true);
echo "<pre>";
print_r($output);
echo "</pre>";
Upvotes: 0
Reputation: 1
Your string \"trackingnumber\":\"'42060106''9405510200830072094975'\"
has syntax error.
$result = '[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json_result = json_decode(stripslashes($result), true);
$json=str_replace("\\",'', $result);
$jsondata=json_decode($json,true);
print_r($jsondata);
Upvotes: 0
Reputation: 494
CURL output is json and in line $json_result = json_decode($result, true); you decoded it to an array, after str_replace you have an array but you decoded it again, may be if you remove $jsondata=json_decode($json,true); and return $json you receive an array
Upvotes: 0
Reputation: 631
$json='[{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"},{\"packagename\":\"Book+-+Outliers\",\"trackingnumber\":\"1Z2FF4063A00030059\",\"packageweight\":\"1.0000\",\"weightunit\":\"Lbs\",\"price\":\"16.9900\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Book+-+David+%26+Goliath+%3B+Face+Cream+-+Clinique%2FGlycolix\",\"trackingnumber\":\"9.36E+21\",\"packageweight\":\"2.0000\",\"weightunit\":\"Lbs\",\"price\":\"18.0000\",\"suiteno\":\"TY1000234\",\"user_id\":\"1000234\"},{\"packagename\":\"Sunglasses+-+Valentino\",\"trackingnumber\":\"1.02E+33\",\"packageweight\":\"0.5000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Sunglasses+-+Safilo+group\",\"trackingnumber\":\"1.01E+33\",\"packageweight\":\"0.8000\",\"weightunit\":\"Lbs\",\"price\":null,\"suiteno\":\"TY1000431\",\"user_id\":\"1000431\"},{\"packagename\":\"Pigmentation+Color+Tobacco\",\"trackingnumber\":\"420601069405510200830072094975\",\"packageweight\":\"0.6300\",\"weightunit\":\"Lbs\",\"price\":\"320.0000\",\"suiteno\":[],\"user_id\":\"NOTFOUND\"}]';
$json=str_replace("\\",'', $json);
$jsondata=json_decode($json,true);
print_r($jsondata);
Upvotes: 1