2016-11-23 4 views
0

私は次の形式のJSONオブジェクトを持っている:エラー(Java)の

{ 
"_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

将来のためのヒント。 getterと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オブジェクトを作成してみてください。この方法では、リスト内のすべてのオブジェクトが別のオブジェクトになります。