我正在开发一个有java文件和php文件的应用程序。java文件调用php文件,这些文件在ddbb中执行查询,并以数组php的形式返回结果,但在屏幕上打印出来。在java中,我把它当作一个字符串,我必须把它转换成and数组或集合,但我不知道该怎么做。
php打印的结果示例如下:
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[name] => pepe
[1] => pepe
)
[1] => Array
(
[id] => 2
[0] => 2
[name] => antoñito
[1] => antoñito
)
[2] => Array
(
[id] => 3
[0] => 3
[name] => loló
[1] => loló
)
[3] => Array
(
[id] => 4
[0] => 4
[name] => ñoño
[1] => ñoño
)
[4] => Array
(
[id] => 5
[0] => 5
[name] => Antoñito
[1] => Antoñito
)
[5] => Array
(
[id] => 7
[0] => 7
[name] => José
[1] => José
)
)如果我使用json_encode($the_array),结果如下:
[{"id":"1","0":"1","name":"pepe","1":"pepe"}, {"id":"2","0":"2","name":"anto\u00f1ito","1":"anto\u00f1ito"},{"id":"3","0":"3","name":"lol\u00f3","1":"lol\u00f3"},{"id":"4","0":"4","name":"\u00f1o\u00f1o","1":"\u00f1o\u00f1o"},{"id":"5","0":"5","name":"Anto\u00f1ito","1":"Anto\u00f1ito"},{"id":"7","0":"7","name":"Jos\u00e9","1":"Jos\u00e9"}]谢谢大家
发布于 2012-06-19 16:58:19
您应该为数据选择一种序列化方法。例如,XML、Protocol Buffers或JSON。
我个人建议您使用JSON,因为它是轻量级的,即使对于人类来说也很容易阅读,而且两种语言都有广泛的库可供选择。
在PHP端进行编码
$encoded = json_encode($data);用Jackson在Java端解码
final ObjectMapper objectMapper = new ObjectMapper();
// the better way is to create a custom class with the correct format
final Map<?, ?> decoded = objectMapper.readValue(encoded, Map.class);发布于 2012-06-19 16:57:13
使用一些更标准化的传输格式,比如JSON。PHP端必须使用json_encode()对数组进行编码,而Java端需要一些库来解码它(参见相关的question)。
发布于 2012-06-19 16:55:34
我认为这可能是有用的http://publib.boulder.ibm.com/infocenter/wsmashin/v1r1/index.jsp?topic=/com.ibm.websphere.sMash.doc/using/zero.php/TypeConversion.html
https://stackoverflow.com/questions/11097592
复制相似问题