Skip to content

Instantly share code, notes, and snippets.

@Budincsevity
Last active August 29, 2015 14:26
Show Gist options
  • Save Budincsevity/b9ce878d5fa4e4762c64 to your computer and use it in GitHub Desktop.
Save Budincsevity/b9ce878d5fa4e4762c64 to your computer and use it in GitHub Desktop.
Working with JSON in Android

Working with JSON in Android

Create a JSONObject

JSONObject jObject = new JSONObject(string); 

To get a specific string

String aJsonString = jObject.getString("STRINGNAME"); 

To get a specific boolean

boolean aJsonBoolean = jObject.getBoolean("BOOLEANNAME"); 

To get a specific integer

int aJsonInteger = jObject.getInt("INTEGERNAME");

To get a specific long

long aJsonLong = jObject.getBoolean("LONGNAME");

To get a specific double

double aJsonDouble = jObject.getDouble("DOUBLENAME");

To get a specific JSONArray

JSONArray jArray = jObject.getJSONArray("ARRAYNAME");

To get the items from the array

    for (int i=0; i < jArray.length(); i++) { 
        try { 
            JSONObject oneObject = jArray.getJSONObject(i); 
            // Pulling items from the array 
            String oneObjectsItem = oneObject.getString("STRINGNAMEinTHEarray"); 
            String oneObjectsItem2 = oneObject.getString("anotherSTRINGNAMEINtheARRAY"); 
        } catch (JSONException e) { 
              // Oops 
        } 
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment