我需要将此 json 转换为 php 数组,然后循环遍历这些项目并将它们放入相应的列中输出 :
[{"roomType":"Single rooms","roomTypeCode":"SB"},{"roomType":"Double rooms","roomTypeCode":"DB"},{"roomType":"Twin rooms","roomTypeCode":"TB"},{"roomType":"Triple rooms","roomTypeCode":"TR"},{"roomType":"Suites","roomTypeCode":"S"},{"roomType":"Non-smoking rooms","roomTypeCode":"NS"},{"roomType":"Quad rooms","roomTypeCode":"Q"},{"roomType":"Twin for sole use","roomTypeCode":"TS"}]
如果我将需要解码的字符串全部打印出来,但 json decode 似乎无法将其转换为关联数组,就像我尝试使用它的方式一样,这就是请求
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdatar = curl_exec($ch);
curl_close($ch);
$roomdata = json_decode($rawdatar);
print $roomdata;
如果我打印 $rawdatar 我得到前面提到的 json 行,但当我打印 $roomdata 时除了数组什么都没有我做错了什么