我有一个JSONObject
this
我写了一个JSON
解析器,但它不起作用。我知道问题出在解析器中,因为我的列表没有发送任何空错误。这是我的解析器:
class exTourParser {
ArrayList<GroupTour> Parse(JSONArray mainArray){
final String SITE_NAME="sitename";
final String TOURNOMENT_SPEED = "speed";
final String HOUR = "hour";
final String BUY_IN = "buyin";
final String MINUTE = "min";
final String REPLAY = "replay";
final String GAME_MODE = "gamemode";
final String PRIZE = "prize";
final String DAY = "day";
final String REBUY = "rebuy";
final String LATE_REG = "latereg";
final String ADD_ON = "addon";
final String TIME = "time";
ArrayList<GroupTour> groupTours = new ArrayList<>();
for (int i =0 ;i>mainArray.length();i++){
try {
JSONArray subArray = mainArray.getJSONArray(i);
for (int j = 0; j>subArray.length(); j++){
JSONObject object = subArray.getJSONObject(i);
ChildTour child =new ChildTour();
GroupTour group = new GroupTour();
child.setSiteName(object.getString(SITE_NAME));
child.setTournomentSpeed(object.getString(TOURNOMENT_SPEED));
child.setHour(object.getInt(HOUR));
child.setMin(object.getInt(MINUTE));
child.setBuyIn(object.getString(BUY_IN));
child.setReply(object.getString(REPLAY));
child.setGameMode(object.getString(GAME_MODE));
child.setPrize(object.getString(PRIZE));
child.setDay(object.getString(DAY));
child.setReBuy(object.getString(REBUY));
child.setLateReg(object.getInt(LATE_REG));
child.setAddon(object.getString(ADD_ON));
child.setTime(object.getString(TIME));
group.setChildNumbers(subArray.length());
group.setName(object.getString(SITE_NAME));
group.setTours(child);
groupTours.add(group);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return groupTours;
}
}
列表是ExpandableListView
,“tours”数组的对象是组,这些对象的对象是子对象
这就是我如何解析它。
private void prepareData(String url, final ExpandableListView listView) {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("tours");
exTourParser parser = new exTourParser();
ArrayList<GroupTour> items = parser.Parse(array);
exTourListAdapter adapter = new exTourListAdapter(getContext(),items);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
quew.add(request);
}
答案 0 :(得分:2)
您的外部是 JSONObject 而不是 JSONArray 。解析如下。
private void parse(String jsonString){
try{
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray dataArray=jsonObject.getJSONArray("tours");
for (int i = 0; i < dataArray.length(); i++) {
JSONArray obj = dataArray.getJSONArray(i);
for (int j = 0; j < obj.length(); j++) {
JSONObject obj2 = obj.getJSONObject(i);
// Parse here
}
}
}catch (JSONException e){
e.printStackTrace();
}
}
答案 1 :(得分:1)
使用Gson Library解析这个json最简单的方法
compile 'com.google.code.gson:gson:2.8.1'
使用think来解析http://www.jsonschema2pojo.org/ 你会得到像这样的pojo类 的 1.ToursModel.Java 强>
public class ToursModel {
@SerializedName("tours")
@Expose
private List<List<Tour>> tours = null;
public List<List<Tour>> getTours() {
return tours;
}
public void setTours(List<List<Tour>> tours) {
this.tours = tours;
}
}
<强> 2.Tour.Java 强>
public class Tour {
@SerializedName("sitename")
@Expose
private String sitename;
@SerializedName("speed")
@Expose
private String speed;
@SerializedName("hour")
@Expose
private Integer hour;
@SerializedName("min")
@Expose
private Integer min;
@SerializedName("buyin")
@Expose
private String buyin;
@SerializedName("replay")
@Expose
private String replay;
@SerializedName("gamemode")
@Expose
private String gamemode;
@SerializedName("prize")
@Expose
private String prize;
@SerializedName("day")
@Expose
private String day;
@SerializedName("rebuy")
@Expose
private String rebuy;
@SerializedName("latereg")
@Expose
private Integer latereg;
@SerializedName("addon")
@Expose
private String addon;
@SerializedName("time")
@Expose
private String time;
@SerializedName("tournamet")
@Expose
private String tournamet;
public String getSitename() {
return sitename;
}
public void setSitename(String sitename) {
this.sitename = sitename;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public Integer getHour() {
return hour;
}
public void setHour(Integer hour) {
this.hour = hour;
}
public Integer getMin() {
return min;
}
public void setMin(Integer min) {
this.min = min;
}
public String getBuyin() {
return buyin;
}
public void setBuyin(String buyin) {
this.buyin = buyin;
}
public String getReplay() {
return replay;
}
public void setReplay(String replay) {
this.replay = replay;
}
public String getGamemode() {
return gamemode;
}
public void setGamemode(String gamemode) {
this.gamemode = gamemode;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getRebuy() {
return rebuy;
}
public void setRebuy(String rebuy) {
this.rebuy = rebuy;
}
public Integer getLatereg() {
return latereg;
}
public void setLatereg(Integer latereg) {
this.latereg = latereg;
}
public String getAddon() {
return addon;
}
public void setAddon(String addon) {
this.addon = addon;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTournamet() {
return tournamet;
}
public void setTournamet(String tournamet) {
this.tournamet = tournamet;
}
}
在解析了json之后,你必须像这样使用解析
Gson mGson=new Gson;
ToursModel toursModel=new ArrayList();
example=mGson.fromJson("json object string",ToursModel.class);
toursModel.get(0).getSitename();
如上所述,您可以访问响应pojo。
答案 2 :(得分:0)
试试这个
JSONObject jsonObject = new JSONObject(response);
try {
JSONArray jsonArray = jsonObject.getJSONArray("tours");
Log.e("=>", "" + jsonArray);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray value = jsonArray.getJSONArray(i);
for (int j = 0; j < value.length(); j++) {
JSONObject obj2 = value.getJSONObject(i);
String sitename = obj2.getString("sitename");
String speed = obj2.getString("speed");
String hour = obj2.getString("hour");
String min = obj2.getString("min");
String buyin = obj2.getString("buyin");
String replay = obj2.getString("replay");
String gamemode = obj2.getString("gamemode");
String prize = obj2.getString("prize");
String day = obj2.getString("day");
String rebuy = obj2.getString("rebuy");
String latereg = obj2.getString("latereg");
String addon = obj2.getString("addon");
String time = obj2.getString("time");
}
}
} catch (JSONException e) {
e.printStackTrace();
}