Tag Archives: PHP

PHP:json_encode返回的可不一定是json object

用PHP做移动端数据接口,以json_encode返回json object数据,今天安卓端的同事跟我反映收到一个空的Array,导致客户端崩溃。空的Array在json_encode的时候难道不会被转为json object么? 查了一下还真是这样。如果不加任何选项,空的Array默认返回的还是空Array,想要全部转为json object,必须加JSON_FORCE_OBJECT选项作为第二参数。 下面是php官网摘录的具体选项的用法 下面是对应的output Normal: [“”,”‘bar'”,””baz””,”&blong&”,”u00e9″] Tags: [“u003Cfoou003E”,”‘bar'”,””baz””,”&blong&”,”u00e9″] Apos: [“”,”u0027baru0027″,””baz””,”&blong&”,”u00e9″] Quot: [“”,”‘bar'”,”u0022bazu0022″,”&blong&”,”u00e9″] Amp: [“”,”‘bar'”,””baz””,”u0026blongu0026″,”u00e9″] Unicode: [“”,”‘bar'”,””baz””,”&blong&”,”é”] All: [“u003Cfoou003E”,”u0027baru0027″,”u0022bazu0022″,”u0026blongu0026″,”é”] Empty array output as array: [] Empty array output as object: {} Non-associative array output as array: [[1,2,3]] Non-associative array output as object: {“0”:{“0″:1,”1″:2,”2”:3}} Associative array always output as object: {“foo”:”bar”,”baz”:”long”} Associative array always […]