Jua*_*njo 0 php java arrays multidimensional-array
我正在开发一个包含java文件和php文件的应用程序.java文件调用php文件,这些文件在ddbb中执行查询并将结果作为数组php返回,但是将其打印在屏幕上.我把它作为一个字符串在java中,我必须将它转换为数组或集合,但我不知道如何做到这一点.
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é
)
)
Run Code Online (Sandbox Code Playgroud)
如果我使用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"}]
Run Code Online (Sandbox Code Playgroud)
感谢大家
您应该为数据选择序列化方法.例如XML,Protocol Buffers或JSON.
我个人建议你使用JSON,因为它很轻巧,即使是人类也很容易阅读,并且有两种语言可供选择的库.
$encoded = json_encode($data);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4958 次 |
| 最近记录: |