JSON parsing:JSON parsing:To parse the data received from http://api.androidhive.info/contacts/1) Create the parent Object based on the brackets. { – JSONObject [ – JSONArray
Object has index provided it is in an JSONArray Object has key provided it is in an JSONObject All variables have keys2) Retrieve the children from its parent4) Create an ArrayList<type of data> and add all the values into ArrayList
ArrayList<String> names = new ArrayList<String>();
try { JSONObject mainObject = new JSONObject(response);
JSONArray contacts = mainObject.getJSONArray(“contacts”);
for (int i = 0; i < contacts.length(); i++) { JSONObject jsonObject = contacts.getJSONObject(i); String id = jsonObject.getString(“id”); String name = jsonObject.getString(“name”); names.add(name); String email = jsonObject.getString(“email”); String address = jsonObject.getString(“address”); String gender = jsonObject.getString(“gender”);
JSONObject phoneObject = jsonObject.getJSONObject(“phone”);
String mobile = phoneObject.getString(“mobile”);
String home = phoneObject.getString(“home”); String office = phoneObject.getString(“office”); } } catch (JSONException e) { e.printStackTrace(); }
Parse the following URLshttp://api.github.com/gistshttps://api.darksky.net/forecast/your_api_key/17.4375,78.4483