9

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.

7
  • 1
    $json='apiBlockTicketRequest'.$data; That's why Commented Jan 22, 2016 at 9:06
  • Look at the expected Json string Commented Jan 22, 2016 at 9:07
  • So that means you start making it by hand? Commented Jan 22, 2016 at 9:07
  • 1
    $json='{apiBlockTicketRequest:'.$data.'}'; should be better. Even though writing your own JSON is generally a bad idea. Just let json_encode do all the work. Commented Jan 22, 2016 at 9:08
  • But Getting the same error Commented Jan 22, 2016 at 9:09

2 Answers 2

19

You creating invalid json by adding apiBlockTicketRequest to output

 $json='apiBlockTicketRequest'.$data;

instead you can do

$json = json_encode(['apiBlockTicketRequest' => $prebook]);
Sign up to request clarification or add additional context in comments.

4 Comments

also if you have php < 5.4 then use array() instead of []
But i don't want { at the begening and end of the string,bcz this json is request of an api service,when i passw this json i wlll get an error from api,
Sorry but then don't use that service because it is asking you to produce invalid JSON
It is required to use { at the begginging so you asking about something that is pseudo json not JSON as @Hanky웃Panky mentioned
0

Your code is correct to make that string but the entire output string ($json) will never parse as json, only the $data you encoded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.