The process of parsing JSON in Android is pretty simple, thankfully. We'll be using JSONObject for all the parsing goodness - there are also some other JSON classes, but we'll just go through the basic ones to give you a general idea of how it works.
- String strJson="your url";
- String data = "";
- try {
- // Create the root JSONObject from the JSON string.
- JSONObject jsonRootObject = new JSONObject(strJson);
- //Get the instance of JSONArray that contains JSONObjects
- JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
- //Iterate the jsonArray and print the info of JSONObjects
- for(int i=0; i < jsonArray.length(); i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- int id = Integer.parseInt(jsonObject.optString("id").toString());
- String name = jsonObject.optString("name").toString();
- float salary = Float.parseFloat(jsonObject.optString("salary").toString());
- }
- output.setText(data);
- } catch (JSONException e)
- {e.printStackTrace();
- }
No comments:
Post a Comment