9

I'm used to PHP, and decoding json data is just a line of code. What would be the easiest way to do this in java?

Amit
  • 103
  • 1
  • 1
  • 5

7 Answers7

8

Pick one of the libraries from the Java section at the bottom of the json.org page.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
6

Gson

Userguide

YOU
  • 120,166
  • 34
  • 186
  • 219
  • 1
    Gson rocks. http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182 – BalusC Dec 18 '09 at 12:23
4

have a look at http://code.google.com/p/json-simple/ maybe it helps ;-)

Hans Sperker
  • 1,347
  • 2
  • 15
  • 37
1

I love Gson, it's very simple and easy to use. If you are interessted in more, here is a tutorial (german): http://blog.mynotiz.de/programmieren/java-json-decode-tutorial-2074/

Frank Roth
  • 6,191
  • 3
  • 25
  • 33
1

Decoding json in java is not too hard. Google's gson api handles json data very well. A tutorial on decoding json data using gson is there in my blog http://preciselyconcise.com/apis_and_installations/json_to_java.php

Sai Sunder
  • 1,001
  • 1
  • 11
  • 16
0

I like Flexjson. It's lightweight and easy to use.

But I admit that I haven't bothered to compare all the alternatives :-)

Eli Acherkan
  • 6,401
  • 2
  • 27
  • 34
0

There are many JSON libraries available in Java.

The most notorious ones are: Jackson, GSON, Genson, FastJson and org.json.

There are typically three things one should look at for choosing any library:

  1. Performance
  2. Ease of use (code is simple to write and legible) - that goes with features.
  3. For mobile apps: dependency/jar size

Specifically for JSON libraries (and any serialization/deserialization libs), databinding is also usually of interest as it removes the need of writing boiler-plate code to pack/unpack the data.

For 1, see this benchmark: https://github.com/fabienrenaud/java-json-benchmark I did using JMH which compares (jackson, gson, genson, fastjson, org.json, jsonp) performance of serializers and deserializers using stream and databind APIs. For 2, you can find numerous examples on the Internet. The benchmark above can also be used as a source of examples...

Quick takeaway of the benchmark: Jackson performs 5 to 6 times better than org.json and more than twice better than GSON.

Let me know if you have any questions.

fabien
  • 1,529
  • 1
  • 15
  • 28