苗条框架中的JSON格式错误

时间:2018-06-26 11:53:18

标签: arrays json slim

我在路线中拥有这个:

echo '{"result":{"code":"200","status":"success","data":'.$response->withJson($rows,200,0).'}}';

返回此JSON:

{"result":{"code":"200","status":"success","data":HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8 [{"id":23,"ragionesociale":"consumatore","indirizzo":"000","comune":"000","provincia":71,"cap":"000","idprovincia_camera":71,"telefono":"000","fax":"000","sitoweb":"0","email":"info@000.it","codice_promo":"xxxxx","password":"xxxxx","data_adesione":"2014-09-26 07:52:23","attivato":1,"costo":0,"tipologia":1,"iva":22,"alias":"prova-it4italy-srl","pagamento":1,"merchantorderid":null,"mybankid":null,"pending":0,"paymentid":null,"logo":"","nome":null,"cognome":null,"sesso":1,"datanascita":null,"eta":null,"nazione":null,"tipologia_utente":1,"fb_token":null,"fb_id":null,"gp_token":null,"gp_id":null,"profile_img":null,"citta":null,"param":null}]}}

问题是我在数据内收到第二个响应标头,这破坏了JSON。 我希望收到这样的回复:

{"result":{"code":"200","status":"success","data":[{"id":23,"ragionesociale":"consumatore","indirizzo":"000",....

是否有苗条的方法不打印JSON中的HTTP/1.1 200 OK Content-Type: application/json;charset=utf-8部分?

1 个答案:

答案 0 :(得分:1)

$response->withJson()中,响应的Content-Type以及默认的200 HTTP状态代码自动设置为application/json;charset=utf-8。因此,您不必手动添加。

请参见response->withJson()

因此,只需执行以下操作即可。

$result = new stdClass(); 
$result->result = new stdClass(); 
$result->result->code = "200";
$result->result->status= "success";
$result->result->data = $rows;

echo $response->withJson($result);