android read json file

      Comments Off on android read json file

android read json file

android read json file

android read json file is most used in mostly all android apps. If you are fetching data from webservice then webservice will mostly return data in json format. You need to extract data from json object. Here is the example of how to extract data from json object.

 

Json File Example

{
  "name": "Adarsh Patel",
  "website": "http://adarshspatel.in",
  "year": 2009
}

 

Android Code to read JSON Object

try {

    JSONObject jsonObject = new JSONObject(jsonAsString);
    String title = jsonObject.getString("name");
    String content = jsonObject.getString("website");
    int year = jsonObject.getInt("year");
    JSONArray names = jsonObject.getJSONArray("name"); //for an array of String objects
} catch (JSONException e) {
    Log.w(TAG,"Error parsing JSON: " + e.getMessage());
}