2016-11-23 91 views
0

我有以下格式的JSON對象:錯誤在Android(Java)的解析JSON

{ 
"_id": "1", 
"trips": [{ 
    "origin": "Spain", 
    "destination": "France" 
}, { 
    "origin": "Italy", 
    "destination": "Germany" 
}, { 
    "origin": "Portugal", 
    "destination": "Ireland" 
}] 
} 

我的目標是分析這個JSON和獲得旅行的ArrayList,對此我有以下代碼:

class Trip { 
    String origin; 
    String destination; 
} 

ArrayList<Trip> tripList; 

public ArrayList<Trip> getTripList(String json){ 

    Trip thisTrip = new Trip(); 
    ArrayList<Trip> thisTripList = new ArrayList<Trip>(); 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray tripArray = jsonObject.getJSONArray("trips"); 

     for(int i = 0; i < tripArray.length(); i++){ 
      JSONObject tripInstance = tripArray.getJSONObject(i); 

      thisTrip.origin = tripInstance.getString("origin"); 
      thisTrip.destination = tripInstance.getString("destination"); 

      thisTripList.add(thisTrip); 
     } 

     return(thisTripList); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return(null); 
    } 
} 

然而,當我執行此方法如下所示,我得到尺寸爲3,這是正確的ArrayList,但是具有所有來源/目的地值相同(即葡萄牙,葡萄牙,葡萄牙;愛爾蘭,愛爾蘭,愛爾蘭)。我究竟做錯了什麼?

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
//... 
     Trip trip = new Trip(); 
     tripList = new ArrayList<Trip>(); 
     tripList = getTripList(json); 
//... 
+0

提示未來。用getters和setter更好地使用模型類 –

回答

3

試試這個。

public ArrayList<Trip> getTripList(String json){ 
    Trip thisTrip; 
    ArrayList<Trip> thisTripList = new ArrayList<Trip>(); 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray tripArray = jsonObject.getJSONArray("trips"); 

     for(int i = 0; i < tripArray.length(); i++){ 
      JSONObject tripInstance = tripArray.getJSONObject(i); 
      thisTrip = new Trip(); 
      thisTrip.origin = tripInstance.getString("origin"); 
      thisTrip.destination = tripInstance.getString("destination"); 

      thisTripList.add(thisTrip); 
     } 

     return(thisTripList); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return(null); 
    } 
} 
+0

謝謝suresh,這解決了我的問題! :) – Victor

2

這裏你正在爲1時創建thisTrip = new Trip();,並且在重新使用它於循環,所以值反映對象,並在陣列中要存儲它也有相同的對象多次,所以你得到相同來自數組的值。

因此,在循環中創建thisTrip = new Trip();。這將解決您的問題。

例子:

for(int i = 0; i < tripArray.length(); i++){ 
      JSONObject tripInstance = tripArray.getJSONObject(i); 

      Trip thisTrip = new Trip(); 

      thisTrip.origin = tripInstance.getString("origin"); 
      thisTrip.destination = tripInstance.getString("destination"); 

      thisTripList.add(thisTrip); 
     } 
0

試試這個簡單的代碼

class Trip { 
    String origin; 
    String destination; 

public Trip(String origin,String destination){ 
     this.origin = origin; 
     this.destination = destination; 
} 

    //getter and setter method here 
} 

ArrayList<Trip> tripList; 

public ArrayList<Trip> getTripList(String json){ 


    tripList = new ArrayList<>(); 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray tripArray = jsonObject.getJSONArray("trips"); 

     for(int i = 0; i < tripArray.length(); i++){ 
      JSONObject tripInstance = tripArray.getJSONObject(i); 
      tripList.add(new Trip(tripInstance.getString("origin"),tripInstance.getString("destination"))); 

     } 

     return tripList; 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

在你onCreate方法

ArrayList<Trip> tripList; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tripList = getTripList(json); 
} 
6

您正在使用每個循環相同行程目標。所以,在arrayList中同樣的對象被引用3次。這就是爲什麼所有3個對象的數據都是相同的。請嘗試以下代碼: -

Trip.java

public class Trip { 

    String origin; 
    String destination; 

    public String getOrigin() { 
     return origin; 
    } 

    public void setOrigin(String origin) { 
     this.origin = origin; 
    } 

    public String getDestination() { 
     return destination; 
    } 

    public void setDestination(String destination) { 
     this.destination = destination; 
    } 
} 

在你的活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_demo); 

    ArrayList<Trip> tripList = new ArrayList<Trip>(); 
    tripList = getTripList(json); 

    Log.e("Trips", "" + tripList.size()); 

} 

public ArrayList<Trip> getTripList(String json) { 

    ArrayList<Trip> thisTripList = new ArrayList<Trip>(); 

    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray tripArray = jsonObject.getJSONArray("trips"); 

     for (int i = 0; i < tripArray.length(); i++) { 

      Trip trip = new Trip(); 
      trip.setOrigin(tripArray.getJSONObject(i).getString("origin")); 
      trip.setDestination(tripArray.getJSONObject(i).getString("destination")); 

      thisTripList.add(trip); 
     } 

     return (thisTripList); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return (null); 
    } 
} 
+0

感謝您的回答,我是一個新手,但你的代碼看起來相當不錯:) – Victor

+0

高興地幫助你:) –

0

的問題是,你正在使用的每個相同的行程目標循環迭代。相同的Trip對象在您的列表中被引用3次。這就是爲什麼所有3個對象的起點和終點都相同。

嘗試創建一個新的Trip對象的每個循環迭代。這樣,列表中的每個對象都是不同的對象。