php array 转换成json字符串,将Array转换为JSON字符串php

我有这样的数组

$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字符串时,我将收到以下错误

Expecting object or array, not string.[Code 1, Structure 1]

Error:Strings should be wrapped in double quotes.

解决方法:

您通过将apiBlockTicketRequest添加到输出来创建无效的json

$json='apiBlockTicketRequest'.$data;

相反,你可以做到

$json = json_encode(['apiBlockTicketRequest' => $prebook]);

标签:json,php,arrays

来源: https://codeday.me/bug/20190716/1475596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值