我正在尝试解码并从此字符串访问JSON对象值,但PHP中的json_decode无法对其进行解码。
{"next_page": "Y", "search_results": [{"name": "Apple iPhone X, Fully
Unlocked 5.8\", 64 GB - Silver", "asin": "B075QN8NDH", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41P889KMNiL._AC_US160_.jpg"},
{"name": "Apple iPhone 8 4.7\", 64 GB, Fully Unlocked, Gold", "asin":
"B075QJSQLH", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41YkkxsF4xL._AC_US160_.jpg"},
{"name": "Apple iPhone X, GSM Unlocked 5.8\", 256 GB - Space Gray", "asin":
"B075QMZH2L", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41YP-wmuPSL._AC_US160_.jpg"},
{"name": "Apple iPhone SE 16 GB Factory Unlocked, Silver (Certified
Refurbished)", "asin": "B01GXAT0D8", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51+VKJJMWAL._AC_US160_.jpg"},
{"name": "Apple iPhone 6 Unlocked Smartphone, 16 GB (Gold) (Certified
Refurbished)", "asin": "B00YD545CC", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51BbI-8wpTL._AC_US160_.jpg"},
{"name": "Apple iPhone 4S 16 GB AT&T, Black", "asin": "B005SSAXSI",
"image_url":
"https://images-na.ssl-images-amazon.com/images/I/41eVAfYDANL._AC_US160_.jpg"},
{"name": "Apple iPhone 7 , GSM Unlocked, 32GB - Rose Gold (Certified
Refurbished)", "asin": "B01N9YOF3R", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/517xB8vSCML._AC_US160_.jpg"},
{"name": "Apple iPhone 8 4.7\", 64 GB, GSM Unlocked, Space Gray", "asin":
"B075QNGDZ9", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41sfP+0QSvL._AC_US160_.jpg"},
{"name": "Apple iPhone 8 a1905 64GB GSM Unlocked (Certified Refurbished)",
"asin": "B078P5BK5G", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41qBAk8HvqL._AC_US160_.jpg"},
{"name": "Apple iPhone 8 Plus 5.5\", 64 GB, Fully Unlocked, Gold", "asin":
"B075QNGHS8", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41AAYX09kZL._AC_US160_.jpg"},
{"name": "Apple iPhone 6S PLUS 16GB (Rose Gold) Factory Unlocked", "asin":
"B015E8VWEU", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/31X7-Lq6FIL._AC_US160_.jpg"},
{"name": "Apple iPhone 6 Plus 16GB GSM Unlocked Smartphone - Silver
(Certified Refurbished)", "asin": "B00YD54HZ2", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51EHOptBcIL._AC_US160_.jpg"},
{"name": "Apple iPhone 6s 128GB Factory Unlocked (Space Gray)", "asin":
"B015E8UKNE", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41NI83uayEL._AC_US160_.jpg"},
{"name": "Huawei Mate 9 with Amazon Alexa and Leica Dual Camera - 64GB
Unlocked Phone - Moonlight Silver (US Warranty)", "asin": "B01N1JV80Z",
"image_url":
"https://images-na.ssl-images-amazon.com/images/I/41qpONRrJBL._AC_US160_.jpg"},
{"name": "Apple iPhone 6s 16GB Space Gray, Verizon Wireless", "asin":
"B075H4C3L4", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/412W-f+wNqL._AC_US160_.jpg"},
{"name": "Honor 6X Unlocked Smartphone, Dual Lens Camera and Dual SIM
Standby, 3GB RAM, 32 GB ROM, Gray", "asin": "B01MRZFBBH", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51y5Cu+v8dL._AC_US160_.jpg"},
{"name": "Apple iPhone 6 (GSM Unlocked), 64GB, Space Gray", "asin":
"B00NQGP5X2", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51We979ipcL._AC_US160_.jpg"},
{"name": "Apple iPhone 6S Plus, GSM Unlocked, 16GB - Silver (Certified
Refurbished)", "asin": "B01JAWWVXW", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41f0V8ZqgML._AC_US160_.jpg"},
{"name": "BLU VIVO X \u2013 6.0\u201d HD+18:9 Display Smartphone with Dual
Front and Rear Cameras \u2013Black", "asin": "B0795W4SRD", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41ftij8famL._AC_US160_.jpg"},
{"name": "Apple iPhone 6S - 128GB GSM Unlocked - Silver (Certified
Refurbished)", "asin": "B01M0L5JFB", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/415ui3Im4kL._AC_US160_.jpg"},
{"name": "Apple iPhone 8 Plus 256gb Black Unlocked", "asin": "B075NVNBCW",
"image_url":
"https://images-na.ssl-images-amazon.com/images/I/31QhJKIvfLL._AC_US160_.jpg"},
{"name": "Apple iPhone 4S 64 GB Unlocked GSM , Black", "asin":
"B005UEF72M", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41S6fyTccFL._AC_US160_.jpg"},
{"name": "HTC One M9 6535L 32GB Verizon Octa-Core Android Smartphone W/ 20MP
Camera - Silver/Rose God (Certified Refurbished)", "asin": "B013QB52VQ",
"image_url":
"https://images-na.ssl-images-amazon.com/images/I/518IUgM7NlL._AC_US160_.jpg"},
{"name": "Apple iPhone 6S Plus 16 GB Unlocked, Gold International Version",
"asin": "B015E8VE6Q", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/41gf4ZVplhL._AC_US160_.jpg"},
{"name": "Apple iPhone 4S 16 GB Factory Unlocked", "asin": "B0149UISAK",
"image_url":
"https://images-na.ssl-images-amazon.com/images/I/41u6Vx+mxRL._AC_US160_.jpg"},
{"name": "Apple iPhone 6 64 GB Unlocked, Gold (Refurbished)", "asin":
"B00VIINO7O", "image_url":
"https://images-na.ssl-images-amazon.com/images/I/51y-UmFbjzL._AC_US160_.jpg"}]}
这个JSON是从一个正在抓亚马逊的python脚本返回的。 我在PHP中使用stripslashes删除斜杠,但json_decode没有返回任何内容。 任何想法怎么做?
答案 0 :(得分:1)
这是因为你的字符串上有新行。
我尝试了下面的代码并且工作正常:
$str = '{"next_page": "Y", "search_results": [{"name": "Ap....'; /* Your json string here*/
$str = preg_replace("/[\r\n]+/", " ", $str);
$arr = json_decode( $str, true );
echo "<pre>";
print_r( $arr );
echo "</pre>";