[英]Errors parsing json on Android (java)
我有以下格式的JSON對象:
{
"_id": "1",
"trips": [{
"origin": "Spain",
"destination": "France"
}, {
"origin": "Italy",
"destination": "Germany"
}, {
"origin": "Portugal",
"destination": "Ireland"
}]
}
我的目標是解析此JSON並獲取Trip的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);
//...
您在每個循環中使用相同的Trip對象。 因此,同一對象在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);
}
}
嘗試這個。
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);
}
}
在這里,您正在創建thisTrip = new Trip();
1次,然后在for循環中重新使用它,因此值反映在對象上,並且要在數組中存儲的對象也多次具有相同的對象,因此從數組中獲取相同的值。
因此,創建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);
}
試試這個更簡單的代碼
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);
}
問題在於,每個循環迭代都使用相同的Trip對象。 同一跳閘對象在列表中被引用3次。 這就是為什么所有3個對象的起點和終點都相同的原因。
嘗試在每個循環迭代中創建一個新的Trip對象。 這樣,列表中的每個對象都是一個不同的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.