In PHP I am trying to put together a JSON string that gets returned with the status of the takes and any errors that may have occurred:
public function create() {
$data = Validate::sanitize($_POST['data']);
parse_str($data);
// Something
$this->JSONResponse[] = $this->addCost($shipmentId, $cost);
$this->JSONResponse[] = '{"code":"0", "type":"info", msg":"Shipment created successfully."}';
return '{"response":['.json_encode($this->JSONResponse).']}';
}
public function addCost($shipmentId, $cost) {
if ($cost > 0) {
// Something
} else {
return '{"code":"1", "type":"info", msg":"Cost not added as it was 0 or left out."}';
}
}
The ways I have tried including the one in the above example don't work. I either get a string that isn't JSON or a JSON object consisting of indexes containing the raw JSON string.
How can I get this to output what I want?
msg", this should be"msg". All json properties must be enclosed by quotes unless they are numbers.