Reputation: 1665
I have an array like this
$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']
)
From this i want to get Json string look like this
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":"[email protected]","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":"[email protected]","idType":"Aadhar Card","idNumber":"jkjk","nameOnId":"hjhj","primary":true,"ac":false,"sleeper":false}],"inventoryType":"0"}
Here is my code
$data =json_encode($prebook);
$json='apiBlockTicketRequest:'.$data;
echo $json;
But when i Validate the JSON string using this I will get the following error
Expecting object or array, not string.[Code 1, Structure 1]
Error:Strings should be wrapped in double quotes.
Upvotes: 9
Views: 41148
Reputation: 1
Your code is correct to make that string
but the entire output string ($json)
will never parse as json
, only the $data
you encoded.
Upvotes: 0
Reputation: 20286
You creating invalid json by adding apiBlockTicketRequest
to output
$json='apiBlockTicketRequest'.$data;
instead you can do
$json = json_encode(['apiBlockTicketRequest' => $prebook]);
Upvotes: 19