0

I am developing an app that has java files and php files. The java files call to the php files, these execute queries in the ddbb and return the result as an array php, but printing it in the screen. I take this in java like an string, and I have to convert it to and array or collection, but I have no idea how to do it.

An example of the result that php prints is:

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é
    )

)

If I use json_encode($the_array) this is the result:

[{"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"}]

Thanks everyone

Juanjo
  • 929
  • 1
  • 15
  • 29

3 Answers3

6

You should select a serialization method for your data. For e.g. XML, Protocol Buffers, or JSON.

I recommend you use JSON because it's lightweight, easy to read even for humans and there are broad libraries available for both languages.

Encoding on the PHP side

$encoded = json_encode($data);

Decoding on the Java side with Jackson

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);
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • I tried that, but when I use the json_enconde I lost each strange character. It seems the utf8 doesnt work with that – Juanjo Jun 19 '12 at 09:13
  • @user1274103 of course it works. JSON serialization escapes non-ASCII characters, so original characters are back when you decode it. – jkrcma Jun 19 '12 at 09:15
  • I have update the question with the result when I apply json_encode – Juanjo Jun 19 '12 at 09:19
  • @user1274103 yes, as you can see special characters are encoded to escape sequences using ASCII characters. This ensures the JSON to be easily and safely transferable through many channels (SMTP, HTTP). When you try to decode it with any tool escape sequences become their former UTF-8 values (ñ, ó, é). Just try to decode it in PHP using `json_decode()`. – jkrcma Jun 19 '12 at 09:56
2

Use some more standardized transport format, like JSON. PHP side has to encode the array using json_encode() and Java side needs some library to decode it (see relevant question).

Community
  • 1
  • 1
jkrcma
  • 1,180
  • 9
  • 14