这是我的示例json字符串data
[{"ratetype":"Y","hotelPackage":"N","roomtype":"Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000015468","ratePlanCode":"0000120709","ratebands":{"validdays":"1111111","wsKey":"fOnrQW3wP9U6eaST9cxEwNldIVM6N6LdxDrDapJ4gq4xUB8SLli5fMQ6w2qSeIKuoaZbT2or8tYkqYfjzPL9wNTRsJN40fffmk9s1Uz6PbbEOsNqkniCrsHjlG4I7MKBayEyj9KbhxGqp8tN23vC5K+lzg+CLhCVc4A727IY+8t1uCj9hv8OAqYPqiatWpH/7ANOsUaHXl4UGSHhnwqssBs1/1meUilvdkgIrhy7u2+1TW+z1jMN3+lQQ1KvLvexxDrDapJ4gq7WpqakyUIlysQ6w2qSeIKu5a8gxPgjph7EOsNqkniCrumRpycuiwkwcJ1QcdikvX8AIeS1LF1SedADwU6eU9IaCaZCAig5yMBccvSRKT+W+ZsfPoLN5c0IliYMwnHbAfXwhKi3O/+L9SyeKeLupHtA9S+XKwz97LvEOsNqkniCrvIh6xV3cNuuKS12MjOBUQSs4y2qNXPFKw==","extGuestTotal":"0","roomTotal":"4350","servicetaxTotal":"466","discount":"385.0","commission":"0","originalRoomTotal":"3965"}},{"ratetype":"Y","hotelPackage":"N","roomtype":"Super Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000017101","ratePlanCode":"0000120713","ratebands":{"validdays":"1111111","wsKey":"mPRyZ7i+l3d9YkBTbyd4CsQ6w2qSeIKubc0g8TGBVVSbBYEQK7zdOgbJJqjW5wOwiG065xYCyu+vVu5bgidY3mfECYFCkMSIn4KACQv3zPbHDVCqduGZ/cYclnMQGbwGDufqO8lYtnyH9KjpH
这是ajax请求
$.ajax({
url:'session.php',
data:{userdata:JSON.stringify(data)},
method:"POST",
success:function(data){
alert(data);
console.log(data);
},
error:function(err){
alert('error'+err);
}
});
在php中
$arr=json_decode($_POST['userdata']);
foreach($arr as $v){
print_r($v);
}
获取此错误
为foreach提供的参数无效
答案 0 :(得分:1)
您想要执行以下操作:
foreach($arr->data as $mydata) {
echo $mydata->ratetype . "\n";
}
将ratetype
替换为您要查找的相应数据。
答案 1 :(得分:0)
您的json语法中有错误
你的json应该是
var data = '[{"ratetype":"Y","hotelPackage":"N","roomtype":"Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000015468","ratePlanCode":"0000120709","ratebands":{"validdays":"1111111","wsKey":"fOnrQW3wP9U6eaST9cxEwNldIVM6N6LdxDrDapJ4gq4xUB8SLli5fMQ6w2qSeIKuoaZbT2or8tYkqYfjzPL9wNTRsJN40fffmk9s1Uz6PbbEOsNqkniCrsHjlG4I7MKBayEyj9KbhxGqp8tN23vC5K+lzg+CLhCVc4A727IY+8t1uCj9hv8OAqYPqiatWpH/7ANOsUaHXl4UGSHhnwqssBs1/1meUilvdkgIrhy7u2+1TW+z1jMN3+lQQ1KvLvexxDrDapJ4gq7WpqakyUIlysQ6w2qSeIKu5a8gxPgjph7EOsNqkniCrumRpycuiwkwcJ1QcdikvX8AIeS1LF1SedADwU6eU9IaCaZCAig5yMBccvSRKT+W+ZsfPoLN5c0IliYMwnHbAfXwhKi3O/+L9SyeKeLupHtA9S+XKwz97LvEOsNqkniCrvIh6xV3cNuuKS12MjOBUQSs4y2qNXPFKw==","extGuestTotal":"0","roomTotal":"4350","servicetaxTotal":"466","discount":"385.0","commission":"0","originalRoomTotal":"3965"}},{"ratetype":"Y","hotelPackage":"N","roomtype":"Super Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000017101","ratePlanCode":"0000120713","ratebands":{"validdays":"1111111","wsKey":"mPRyZ7i+l3d9YkBTbyd4CsQ6w2qSeIKubc0g8TGBVVSbBYEQK7zdOgbJJqjW5wOwiG065xYCyu+vVu5bgidY3mfECYFCkMSIn4KACQv3zPbHDVCqduGZ/cYclnMQGbwGDufqO8lYtnyH9KjpH"}}]';
在html中
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(function(){
$('body').on('click','.ajax',function(){
var id = '[{"ratetype":"Y","hotelPackage":"N","roomtype":"Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000015468","ratePlanCode":"0000120709","ratebands":{"validdays":"1111111","wsKey":"fOnrQW3wP9U6eaST9cxEwNldIVM6N6LdxDrDapJ4gq4xUB8SLli5fMQ6w2qSeIKuoaZbT2or8tYkqYfjzPL9wNTRsJN40fffmk9s1Uz6PbbEOsNqkniCrsHjlG4I7MKBayEyj9KbhxGqp8tN23vC5K+lzg+CLhCVc4A727IY+8t1uCj9hv8OAqYPqiatWpH/7ANOsUaHXl4UGSHhnwqssBs1/1meUilvdkgIrhy7u2+1TW+z1jMN3+lQQ1KvLvexxDrDapJ4gq7WpqakyUIlysQ6w2qSeIKu5a8gxPgjph7EOsNqkniCrumRpycuiwkwcJ1QcdikvX8AIeS1LF1SedADwU6eU9IaCaZCAig5yMBccvSRKT+W+ZsfPoLN5c0IliYMwnHbAfXwhKi3O/+L9SyeKeLupHtA9S+XKwz97LvEOsNqkniCrvIh6xV3cNuuKS12MjOBUQSs4y2qNXPFKw==","extGuestTotal":"0","roomTotal":"4350","servicetaxTotal":"466","discount":"385.0","commission":"0","originalRoomTotal":"3965"}},{"ratetype":"Y","hotelPackage":"N","roomtype":"Super Deluxe Double Room","roombasis":",Complimentary Wi-Fi Internet, Breakfast, ","roomTypeCode":"0000017101","ratePlanCode":"0000120713","ratebands":{"validdays":"1111111","wsKey":"mPRyZ7i+l3d9YkBTbyd4CsQ6w2qSeIKubc0g8TGBVVSbBYEQK7zdOgbJJqjW5wOwiG065xYCyu+vVu5bgidY3mfECYFCkMSIn4KACQv3zPbHDVCqduGZ/cYclnMQGbwGDufqO8lYtnyH9KjpH"}}]';
$.ajax({
url : 'server.php',
data : {"da":id},
method : "POST",
success:function(data)
{
$(".result").html(data);
},
error:function(err)
{
alert('error'+err);
}
});
});
});
</script>
</head>
<body>
<input type="button" class="ajax" name="details" value="details">
<div class="result">
</div>
</body>
</html>
在php中使用
<?php
$arr= json_decode($_POST['da']);
foreach($arr as $v)
{
echo "<pre>";
print_r($v);
echo "</pre>";
}
?>