如何将API响应转换为PHP数组?

时间:2016-11-21 07:08:23

标签: php arrays json

我从API调用中获取此数据。

  

[{\ “软件包名\”:\ “书+ - +离群\”,\ “trackingnumber \”:\ “1Z2FF4063A00030059 \”,\ “packageweight \”:\ “1.0000 \”,\ “weightunit \”: \ “磅\”,\ “价格\”:\ “16.9900 \”,\ “suiteno \”:[],\ “USER_ID \”:\ “NOTFOUND \”},{\ “软件包名\”:\“书+ - +离群\ “\ ”trackingnumber \“:\ ”1Z2FF4063A00030059 \“,\ ”packageweight \“:\ ”1.0000 \“,\ ”weightunit \“:\ ”磅\“,\ ”价格\“:\” 16.9900 \ “\ ”suiteno \“:\ ”TY1000234 \“,\ ”USER_ID \“:\ ”1000234 \“},{\ ”软件包名\“:\” 书+ - +大卫+%26 +巨人+%3B +面部+霜+ - +倩碧%2FGlycolix \”,\ “trackingnumber \”:\ “9.36E + 21 \”,\ “packageweight \”:\ “2.0000 \”,\ “weightunit \”:\ “磅\”,\ “价格\”:\ “18.0000 \”,\ “suiteno \”:\ “TY1000234 \”,\ “USER_ID \”:\ “1000234 \”},{\ “软件包名\”:\“太阳镜+ - +瓦伦蒂诺\ ” \ “trackingnumber \”:\ “1.02E + 33 \”,\ “packageweight \”:\ “0.5000 \”,\ “weightunit \”:\ “磅\”,\ “价格\”:空,\ “suiteno \”:\ “TY1000431 \”,\ “USER_ID \”:\ “1000431 \”},{\ “软件包名\”:\ “太阳镜+ - +霞飞+组\”,\ “trackingnumber \”:\” 1.01E + 33 \ “\ ”packageweight \“:\ ”0.8000 \“,\ ”weightunit \“:\ ”磅\“,\ ”价格\“:空,\ ”suiteno \“:\” TY10 00431 \”,\ “USER_ID \”:\ “1000431 \”},{\ “软件包名\”:\ “色素+ +颜色烟草\”,\ “trackingnumber \”:\ “ '42060106''9405510200830072094975' \” ,\ “packageweight \”:\ “0.6300 \”,\ “weightunit \”:\ “磅\”,\ “价格\”:\ “320.0000 \”,\ “suiteno \”:[],\“USER_ID \ “:\” NOTFOUND \“}]

如何将此数据转换为 PHP数组?我尝试使用json_decode($result,true)但它无法正常工作。提前谢谢。

更新:

我的PHP代码

$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;

5 个答案:

答案 0 :(得分:2)

使用json_decode()

// 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

Reference

答案 1 :(得分:1)

  $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);

答案 2 :(得分:0)

试试这个,

$jsonData = stripslashes(html_entity_decode($result));     
$output = json_decode($jsonData, true);

echo "<pre>";
print_r($output);
echo "</pre>";

或尝试这种方式,

$out = preg_replace('/\\\"/',"\"", $result);
$output = json_decode($out, true);

echo "<pre>";
print_r($output);
echo "</pre>";

答案 3 :(得分:0)

CURL输出是json并且在行$ json_result = json_decode($ result,true);你把它解码成一个数组,在str_replace之后你有一个数组但你再次解码它,可能是你删除了$ jsondata = json_decode($ json,true);并返回$ json你收到一个数组

答案 4 :(得分:0)

您的字符串\"trackingnumber\":\"'42060106''9405510200830072094975'\"有语法错误。

$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);