0

How i can parse this type of json array?

{
    "Success": "Yes",
    "FuelPrice": {
        "Coastal": {
            "Petrol": "R12.53",
            "Diesel": "R13.10"
        },
        "Inland": {
            "Petrol": "R12.85",
            "Diesel": "R 13.85"
        }
    }
}
Avijit
  • 3,834
  • 4
  • 33
  • 45
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

1 Answers1

2

Use following function to parse your JSONObject.

public static JSONObject getJSONObjectfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jObject = null;

        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            // Log.e("result:",""+result);

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jObject = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jObject;
    }
DreamsNeverDie
  • 542
  • 2
  • 6
  • 14