views:

317

answers:

5

I need a JSON, JS Array parser in Java and/or PHP. Do you know any parser ?

regards,

+5  A: 

PHP can decode JSON objects already... use json_decode().

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
Andrew Moore
And a JavaScript array. Something like [["apples", "fruit", "red"], ["broccoli", "vegetable", "green"], ["cherries","fruit", "red"]] ?
Arthur Ronald F D Garcia
**@Arthur Ronald F D Garcia:** No problem, `json_decode()` will work as well for that JSON string.
Andrew Moore
+6  A: 

json.org lists a whole slew of them for both languages.

The java one is here: http://www.json.org/java/index.html

PHP 5.2 has it bundled already.

seth
+1  A: 

json_decode() and json_encode() will do the trick, as long as your text is UTF-8.

Nicolas
Does it work fine in ISO-8859-1 ?
Arthur Ronald F D Garcia
**@Arthur Ronald F D Garcia:** you can always `utf8_encode()` your string before passing it to `json_decode()`.
Andrew Moore
It always has for me.
Eli
Anyway, ISO-8859-1 can be somewhat considered as a subset of UTF-8. You won't have any problems using `json_decode()`. Problems might arise if you are using `json_encode()` though on a ISO-8859-1 website.
Andrew Moore
+1  A: 

FlexJson is not too bad for Java, it even allows for typing of the JSON. I didn't really like the json.org Java libs serialization, it required a lot of coding.

stevedbrown
A: 

If you are using PHP >= 5.2, you can use json_decode (as said in other answers).

In PHP 5.1.x, I have successfully used Zend_Json both to encode and decode JSON data -- it does not require the whole Zend Framework to work (If I remember correctly, it requires Zend_Exception, and that's it)

Pascal MARTIN