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.
The json_encode() function can return a string containing the JSON representation of supplied value. The encoding is affected by supplied options, and additionally, the encoding of float values depends on the value of serialize_precision.
json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).
To convert an array to a string, one of the common ways to do that is to use the json_encode() function which is used to returns the JSON representation of a value. This function takes any value as input except resource.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
You creating invalid json by adding apiBlockTicketRequest
to output
$json='apiBlockTicketRequest'.$data;
instead you can do
$json = json_encode(['apiBlockTicketRequest' => $prebook]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With