Parse JSON with org.json [duplicate] Parse JSON with org.json [duplicate] json json

Parse JSON with org.json [duplicate]


1) Assuming you have the JSON libraries on your path (from www.json.org), it's pretty easy.

import org.json.JSONTokener;...URI uri = new URI("http://someserver/data.json");JSONTokener tokener = new JSONTokener(uri.toURL().openStream());JSONObject root = new JSONObject(tokener);

From there, you can address the various parts of the JSON object. Take a look at the Javadocs for the specifics. https://developer.android.com/reference/org/json/package-summary.html


Here is the most universal solution, which allows to parse any JSON type into appropriate Java type:

Object json = new JSONTokener(response).nextValue();

Then you can determine resulting type and handle it appropriately.


I'd pass it

map<String, Object> 

loaded with

map<String, Object> 

in the object field.

Basically recreating the hierarchy of your java classes inside of a large map.

Example :

return ( Map<"GetFolderFilesByZoneResult", Map<"Result1", (object by id 98748) | "Result2", (object by id 99107) | "Result3", etc.

JSON will return that big map very pretty like, and programmatically it's easier to do then lists.