Skip to content

Instantly share code, notes, and snippets.

@devrath
Created November 13, 2014 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/7121bb9ebdbbe310e959 to your computer and use it in GitHub Desktop.
Save devrath/7121bb9ebdbbe310e959 to your computer and use it in GitHub Desktop.
Parsing JSON using GSON parser
// Refered Link: http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java
// Refered Link: http://stackoverflow.com/a/10593838/1083093
JSONObject refJson;
JSONArray jsonarray= null;
JsonElement gsonJelement;
JsonObject gsonJobject;
try {
// Retrieve JSON Objects from the given URL address
refJson = JSONfunctions.getJSONfromURL(url.trim());
gsonJelement = new JsonParser().parse(refJson.toString());
gsonJobject = gsonJelement.getAsJsonObject();
gsonJobject = gsonJobject.getAsJsonObject("locations");
refJson = new JSONObject(gsonJobject.toString().trim());
Iterator<?> keys = refJson.keys();
while( keys.hasNext() ){
String key = (String)keys.next();
//jsonobject=jsonObj.getAsJsonObject("locations");
gsonJelement = new JsonParser().parse(refJson.toString());
gsonJobject = gsonJelement.getAsJsonObject();
gsonJobject = gsonJobject.getAsJsonObject(key);
refJson = new JSONObject(gsonJobject.toString().trim());
String refLatitude=refJson.getString("latitude");
String refLongitude=refJson.getString("longitude");
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment