错误在Android(Java)的解析JSON
我有以下格式的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);
//...
试试这个。
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);
}
}
谢谢suresh,这解决了我的问题! :) – Victor
这里你正在为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);
}
试试这个简单的代码
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);
}
您正在使用每个循环相同行程目标。所以,在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);
}
}
感谢您的回答,我是一个新手,但你的代码看起来相当不错:) – Victor
高兴地帮助你:) –
的问题是,你正在使用的每个相同的行程目标循环迭代。相同的Trip对象在您的列表中被引用3次。这就是为什么所有3个对象的起点和终点都相同。
尝试创建一个新的Trip对象的每个循环迭代。这样,列表中的每个对象都是不同的对象。
提示未来。用getters和setter更好地使用模型类 –