2017-06-20 63 views
-1

我有一個JSON文件,我想解析但不知道如何正確訪問它。Android Studio:JSON解析

它不以一個對象括號「{」開頭,之後有一個名稱,例如, 「演員:」「[」....]}

我可以輕鬆創建一個 JSONObject jObj = new JSONObject(data); JSONArray jArray = jObj.getJSONArray(「actors」);

礦山看起來更像這個

[ 
{ 
"type": "fuel", 
"name": "Aral", 
"address": "Somestreet 65", 
"lat": 49.8848387, 
"lon": 8.6520691 }, 
{ 
"type": "amenity", 
"name": "Centralstation", 
"address": "Centralstreet 20", 
"lat": 49.8725, 
"lon": 8.628889, 
"icon": "somepicture.jpg" }, 
] 

我想是這樣

try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(params[0]); 
      HttpResponse response = client.execute(post); 

      int status = response.getStatusLine().getStatusCode(); 

      if(status == 200){ 
       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity); 

       JSONArray jsonArray = new JSONArray(data); 
       //JSONObject jsonObject = new JSONObject(data); 
       for(int i=0; i< jsonArray.length();i++){ 
        Locations location = new Locations(); 
        JSONObject jRealObject = jsonArray.getJSONObject(i); 
        location.setName(jRealObject.getString("type")); 
        location.setName(jRealObject.getString("name")); 
        location.setName(jRealObject.getString("address")); 
        location.setName(jRealObject.getString("lat")); 
        location.setName(jRealObject.getString("lon")); 
        //location.setImage(jRealObject.getString("icon")); 
        locationList.add(location); 
       } 
       return true; 
      } 

     }catch (ClientProtocolException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }catch (JSONException e){ 
      e.printStackTrace(); 
     } 
     return false; 
    } 

但有一個錯誤在解析它,我認爲它是與「JSONArray jsonArray =新JSONArray( 「」);

你能幫我一下,或者指點一下我能找到我的錯誤的方向嗎

+0

初始化Json數組爲:JSONArray jasnArray = new JSONArray(); – Akshay

+0

對我不起作用 –

回答

0
JSONArray jarray=new JSONArray(data); 
for (int i=0;i<=jarray.length();i++) 
    { 
     JSONObject obj1=jarray.getJSONObject(i); 
     String address=obj1.getString("type"); 
     String caseno=obj1.getString("name"); 
     String casetype=obj1.getString("address"); 
    } 
+0

我還可以使用我的locationList數組,以便不必執行String address = obj1.getString(「type」)? 我試過你的例子,但它仍然不工作 –

0

試試這個....

  JSONArray jsonArray = new JSONArray (data); 
        for(int i=0; i< jsonArray.length();i++){ 
         Locations location = new Locations(); 
         JSONObject jRealObject = jsonArray.getJSONObject(i); 
         location.setName(jRealObject.getString("type")); 
         location.setName(jRealObject.getString("name")); 
         location.setName(jRealObject.getString("address")); 
         location.setName(jRealObject.getString("lat")); 
         location.setName(jRealObject.getString("lon")); 
         locationList.add(location); 
        } 

- 如果你得到一個不同的充鍵使用迭代器取得鑰匙像

Iterator<String> iter = json.keys(); 
while (iter.hasNext()) { 
    String key = iter.next(); 
    try { 
     Object value = json.get(key); 
    } catch (JSONException e) { 
     // Something went wrong! 
    } 
} 
+0

感謝您的回覆,但它仍然無法正常工作 –