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?
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.
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 ).
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.
Pick one of the libraries from the Java section at the bottom of the json.org page.
Gson
Userguide
have a look at http://code.google.com/p/json-simple/ maybe it helps ;-)
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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With