【发布时间】:2016-04-28 19:39:27
【问题描述】:
我有一个这样的数组
$prebook=array(
'sourceCity'=>$_POST['source'],
'destinationCity'=>$_POST['dest'],
'doj'=>$_POST['doj'],
'routeScheduleId'=>$_POST['routeid'],
'boardingPoint'=>array(
'id'=>$id,
'location'=>$location,
'time'=>$time
),
'customerName'=>$_POST['fname'],
'customerLastName'=>$_POST['lname'],
'customerEmail'=>$_POST['email'],
'customerPhone'=>$_POST['mobileno'],
'emergencyPhNumber'=>$_POST['emc-number'],
'customerAddress'=>$_POST['address'],
'blockSeatPaxDetails'=>array(array(
'age'=>$_POST['age'][$key],
'name'=>$value,
'seatNbr'=>$_POST['seat-no'][$key],
'Sex'=>$_POST['gender'.$no],
'fare'=>$_POST['base-fare'][$key],
'totalFareWithTaxes'=>$_POST['amount'][$key],
'ladiesSeat'=>$ladies,
'lastName'=>$_POST['plname'][$key],
'mobile'=>$_POST['mobileno'],
'title'=>'Mr',
'email'=>$_POST['email'],
'idType'=>$_POST['idtype'],
'idNumber'=>$_POST['id-number'],
'nameOnId'=>$value,
'primary'=>true,
'ac'=>$ac,
'sleeper'=>$sleeper
)),
'inventoryType'=>$_POST['invtype']
)
从这里我想得到 Json 字符串看起来像这样
apiBlockTicketRequest:{"sourceCity":"Hyderabad","destinationCity":"Bangalore","doj":"2016-01-22","routeScheduleId":"6717","boardingPoint":{"id":"2889","location":"Mettuguda,Opp. Mettuguda Church","time":"04:50PM"},"customerName":"jj","customerLastName":"jjj","customerEmail":"shamonsha665@gmail.com","customerPhone":"7779","emergencyPhNumber":"7878","customerAddress":"gjgj","blockSeatPaxDetails":[{"age":"22","name":"hjhj","seatNbr":"G4","Sex":"F","fare":"900","totalFareWithTaxes":"945","ladiesSeat":false,"lastName":"hjhj","mobile":"7779","title":"Mr","email":"shamonsha665@gmail.com","idType":"Aadhar Card","idNumber":"jkjk","nameOnId":"hjhj","primary":true,"ac":false,"sleeper":false}],"inventoryType":"0"}
这是我的代码
$data =json_encode($prebook);
$json='apiBlockTicketRequest:'.$data;
echo $json;
但是当我使用this 验证 JSON 字符串时,我会收到以下错误
期望对象或数组,而不是字符串。[代码 1,结构 1]
错误:字符串应该用双引号括起来。
【问题讨论】:
-
$json='apiBlockTicketRequest'.$data;这就是为什么 -
查看预期的Json字符串
-
也就是说你开始手工制作了?
-
$json='{apiBlockTicketRequest:'.$data.'}';应该更好。尽管编写自己的 JSON 通常是个坏主意。让json_encode完成所有工作。 -
但得到同样的错误