slim 框架中格式错误 JSON
Malformed JSON in slim framework
我在路线中有这个:
echo '{"result":{"code":"200","status":"success","data":'.$response->withJson($rows,200,0).'}}';
那个 returns 这个 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}]}}
问题是我在数据中收到了第二个响应 header,这打破了 JSON。
我想要这样的回复:
{"result":{"code":"200","status":"success","data":[{"id":23,"ragionesociale":"consumatore","indirizzo":"000",....
在 slim 中有没有办法不打印 JSON 中的 HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
部分?
在 $response->withJson()
中,响应的内容类型自动设置为 application/json;charset=utf-8
,以及默认的 200 HTTP 状态代码。所以你不必手动添加它。
所以只需执行以下操作就足够了。
$result = new stdClass();
$result->result = new stdClass();
$result->result->code = "200";
$result->result->status= "success";
$result->result->data = $rows;
echo $response->withJson($result);
我在路线中有这个:
echo '{"result":{"code":"200","status":"success","data":'.$response->withJson($rows,200,0).'}}';
那个 returns 这个 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}]}}
问题是我在数据中收到了第二个响应 header,这打破了 JSON。 我想要这样的回复:
{"result":{"code":"200","status":"success","data":[{"id":23,"ragionesociale":"consumatore","indirizzo":"000",....
在 slim 中有没有办法不打印 JSON 中的 HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
部分?
在 $response->withJson()
中,响应的内容类型自动设置为 application/json;charset=utf-8
,以及默认的 200 HTTP 状态代码。所以你不必手动添加它。
所以只需执行以下操作就足够了。
$result = new stdClass();
$result->result = new stdClass();
$result->result->code = "200";
$result->result->status= "success";
$result->result->data = $rows;
echo $response->withJson($result);