2013-12-18 111 views
0

如何解析这种类型的json数组?Android解析json数组

{ 
    "Success": "Yes", 
    "FuelPrice": { 
     "Coastal": { 
      "Petrol": "R12.53", 
      "Diesel": "R13.10" 
     }, 
     "Inland": { 
      "Petrol": "R12.85", 
      "Diesel": "R 13.85" 
     } 
    } 
} 
+1

没有json数组 – Raghunandan

+0

对不起我的错误imean对象。 –

+0

json如何存储? – Nfear

回答

2

使用以下函数来解析您的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; 
    }