伙计们正在创建一个基于json格式的表
{"Seat Availability":[{"Ship":"Kavaratti","Departure Date":"10/12/2014"},{"Class":"Bunk","Seats":"178"},{"Class":"Bunk","Seats":"200"},{"Class":"Bunk","Seats":"214"},{"Class":"Bunk","Seats":"246"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"112"},{"Class":"Second","Seats":"120"},{"Class":"Second","Seats":"95"},{"Class":"Second","Seats":"97"},{"Ship":"Kavaratti","Departure Date":"22/12/2014"},{"Class":"Bunk","Seats":"0"},{"Class":"Bunk","Seats":"1"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"10"},{"Class":"Second","Seats":"19"},{"Class":"Second","Seats":"24"},{"Class":"Second","Seats":"6"},{"Ship":"Kavaratti","Departure Date":"03/01/2015"},{"Class":"Bunk","Seats":"229"},{"Class":"Bunk","Seats":"256"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"105"},{"Class":"Second","Seats":"109"}]}
除了最后一批船只细节外,一切正常。我无法理解为什么没有显示最后一艘船的细节。
这是我使用的解析代码
TableLayout ll = (TableLayout) findViewById(R.id.tableLayoutProduct);
JSONObject json = null;
try {
json = new JSONObject(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result = "";
JSONArray arr = null;
try {
arr = json.getJSONArray("Seat Availability");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView heading = (TextView) findViewById(R.id.heading);
heading.setText("Seat Availability: "+destination);
for(int i=0,j=0,z=0; i<arr.length(); i++){
TableRow tbrow1 = new TableRow(HttpExample.this);
TableRow tbrow2 = new TableRow(HttpExample.this);
TableRow tbrow3 = new TableRow(HttpExample.this);
tbrow1.setBackgroundColor(Color.parseColor("#C9D1F7"));
if(i==0){
TextView tv4 = new TextView(HttpExample.this);
tv4.setText(" Ship ");
TextView tv5 = new TextView(HttpExample.this);
tv5.setText(" Departure Date ");
tbrow1.addView(tv4);
tbrow1.addView(tv5);
}
else{
TextView tv3 = new TextView(HttpExample.this);
tv3.setText(" ");
TextView tv4 = new TextView(HttpExample.this);
tv4.setText(" ");
tbrow1.addView(tv3);
tbrow1.addView(tv4);
}
TextView tv1=new TextView(HttpExample.this);
TextView departure_date = new TextView(HttpExample.this);
TextView tv2 = new TextView(HttpExample.this);
TextView index = new TextView(HttpExample.this);
try {
jo = arr.getJSONObject(i);
result = jo.get("Ship").toString();
j++;
tv1.setText(result+" ");
tv1.setEms(5);
tbrow2.addView(tv1);
result = (String) jo.get("Departure Date");
departure_date.setEms(7);
departure_date.setText(result+" ");
tbrow2.addView(departure_date);
i++;
jo = arr.getJSONObject(i);
TextView[] tv_heading = new TextView[9];
TextView[] tv_value = new TextView[9];
while(jo.has("Class"))
{
tv_heading[z] = new TextView(HttpExample.this);
tv_value[z] = new TextView(HttpExample.this);
tv_heading[z].setEms(4);
tv_value[z].setEms(4);
tv_heading[z].setText(jo.get("Class").toString()+" ");
seats = Integer.parseInt(jo.getString("Seats").toString());
if(seats==0)
tv_value[z].setTextColor(Color.RED);
else
tv_value[z].setTextColor(Color.parseColor("#160A98"));
tv_value[z].setText(jo.get("Seats").toString());
tbrow1.addView(tv_heading[z]);
tbrow2.addView(tv_value[z]);
i++;
z++;
jo = arr.getJSONObject(i);
}
i--;
z=0;
TextView blank = new TextView(HttpExample.this);
blank.setText(" ");
tbrow3.addView(blank);
ll.addView(tbrow1);
ll.addView(tbrow2);
ll.addView(tbrow3);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
任何人都可以指出问题
答案 0 :(得分:0)
发现问题..它需要在while循环中使用break语句 在i ++之后 如果(I&GT = arr.length())中断;