Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode JSON data in Java

Tags:

java

json

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?

like image 839
Amit Avatar asked Dec 18 '09 12:12

Amit


People also ask

How do you parse a JSON object in Java?

First, we need to convert the JSON string into a JSON Object, using JSONObject class. Also, note that “pageInfo” is a JSON Object, so we use the getJSONObject method. Likewise, “posts” is a JSON Array, so we need to use the getJSONArray method.

What is JSON parser in Java?

public interface JsonParser extends Closeable. Provides forward, read-only access to JSON data in a streaming way. This is the most efficient way for reading JSON data. The class Json contains methods to create parsers from input sources ( InputStream and Reader ).

Does Java have a built in JSON parser?

Initially I thought I'd go with JSON, but it seems Java doesn't have a built-in parser for JSON. The data stored will be a bunch of records, each record composed of a set of fields. So it's not simple enough to be stored in a text file that can be read line by line.


5 Answers

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

like image 189
moonshadow Avatar answered Oct 04 '22 19:10

moonshadow


Gson

Userguide

like image 29
YOU Avatar answered Oct 04 '22 19:10

YOU


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

like image 44
Hans Sperker Avatar answered Oct 04 '22 19:10

Hans Sperker


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/

like image 30
Frank Roth Avatar answered Oct 04 '22 18:10

Frank Roth


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

like image 20
Sai Sunder Avatar answered Oct 04 '22 20:10

Sai Sunder