首先我不会说英语,请理解。
我使用JAVA和'总线到达系统API',但我遇到了问题。
如果只有一辆公共汽车将到达," item"是一个JSONObject。
但如果有超过2辆公共汽车将到达," item"是一个JSONArray。
无论如何我不知道......如果有任何想法,请帮帮我.. 谢谢。
这是我的代码。
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
no1.setText(""); no2.setText(""); no3.setText(""); no4.setText(""); no5.setText(""); no6.setText(""); no7.setText(""); no8.setText("");
info1.setText(""); info2.setText(""); info3.setText(""); info4.setText(""); info5.setText(""); info6.setText(""); info7.setText(""); info8.setText("");
station1.setText(""); station2.setText(""); station3.setText(""); station4.setText(""); station5.setText(""); station6.setText(""); station7.setText(""); station8.setText("");
time1.setText(""); time2.setText(""); time3.setText(""); time4.setText(""); time5.setText(""); time6.setText(""); time7.setText(""); time8.setText("");
}
private ArrayList<String> arrstation = new ArrayList<>();
private ArrayList<String> arrtime = new ArrayList<>();
private ArrayList<String> busno = new ArrayList<>();
private ArrayList<String> businfo = new ArrayList<>();
private ArrayList<Integer> intarrtime = new ArrayList<>();
public void Bus(){
try{
StringBuilder urlBuilder = new StringBuilder("http://openapi.tago.go.kr/openapi/service/ArvlInfoInqireService/getSttnAcctoArvlPrearngeInfoList"); /*URL*/
urlBuilder.append("?" + URLEncoder.encode("ServiceKey","UTF-8") + "=Tcit3Up%2FxLhDOivx1nH8pb9%2FJB1g44Wy7OC1pISvRlUCt2fnnPJyD0si1jFkvjlYXaixwMD%2BVV5j76M8i%2BEpuA%3D%3D"); /*Service Key*/
urlBuilder.append("&" + URLEncoder.encode("cityCode","UTF-8") + "=" + URLEncoder.encode("32010", "UTF-8")); /*도시코드*/
urlBuilder.append("&" + URLEncoder.encode("nodeId","UTF-8") + "=" + URLEncoder.encode("CCB250001349", "UTF-8")); /*정류소ID*/
urlBuilder.append("&" + URLEncoder.encode("_type", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8"));
URL url = new URL(urlBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "application/json");
System.out.println("Response code: " + conn.getResponseCode());
BufferedReader rd;
if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String result = new String();
String line;
while ((line = rd.readLine()) != null) {
result=result.concat(line);
}
System.out.println(result);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParser.parse(result);
System.out.println("parsing start");
JSONObject response = (JSONObject) jsonObj.get("response");
JSONObject body = (JSONObject) response.get("body");
JSONObject items = (JSONObject) body.get("items");
JSONArray itemArray = (JSONArray) items.get("item");
JSONObject item_array[] = new JSONObject[10];
for(int i=0; i<=8; i++){
item_array[i] = (JSONObject) itemArray.get(i);
busno.add(item_array[i].get("routeno").toString());
businfo.add(item_array[i].get("routetp").toString());
arrtime.add(item_array[i].get("arrtime").toString());
arrstation.add(item_array[i].get("arrprevstationcnt").toString());
intarrtime.add(Integer.parseInt(arrtime.get(i))/60);
}
rd.close();
conn.disconnect();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void setBusLabel(ArrayList<String> busno, ArrayList<String> businfo, ArrayList<Integer> intarrtime, ArrayList<String> arrstation) {
no1.setText(busno.get(0)); no2.setText(busno.get(1)); no3.setText(busno.get(2)); no4.setText(busno.get(3)); no5.setText(busno.get(4)); no6.setText(busno.get(5)); no7.setText(busno.get(6)); no8.setText(busno.get(7));
info1.setText(businfo.get(0)); info2.setText(businfo.get(1)); info3.setText(businfo.get(2)); info4.setText(businfo.get(3)); info5.setText(businfo.get(4)); info6.setText(businfo.get(5)); info7.setText(businfo.get(6)); info8.setText(businfo.get(7));
/*
time1.setText(intarrtime[0].toString()+"분"); time2.setText(intarrtime[1].toString()+"분"); time3.setText(intarrtime[2].toString()+"분"); time4.setText(intarrtime[3].toString()+"분"); time5.setText(intarrtime[4].toString()+"분"); time6.setText(intarrtime[5].toString()+"분"); time7.setText(intarrtime[6].toString()+"분"); time8.setText(intarrtime[7].toString()+"분");
station1.setText(arrstation[0]); station2.setText(arrstation[1]); station3.setText(arrstation[2]); station4.setText(arrstation[3]); station5.setText(arrstation[4]); station6.setText(arrstation[5]); station7.setText(arrstation[6]); station8.setText(arrstation[7]
*/
}
public ArrayList<String> getArrstation() {
return arrstation;
}
public void setArrstation1(ArrayList<String> arrstation) {
this.arrstation = arrstation;
}
public ArrayList<String> getBusno() {
return busno;
}
public void setBusno1(ArrayList<String> busno) {
this.busno = busno;
}
public ArrayList<String> getBusinfo() {
return businfo;
}
public void setBusinfo1(ArrayList<String> businfo) {
this.businfo = businfo;
}
public ArrayList<Integer> getIntarrtime() {
return intarrtime;
}
public void setIntarrtime(ArrayList<Integer> intarrtime) {
this.intarrtime = intarrtime;
}
}
答案 0 :(得分:1)
虽然服务中可能存在设计缺陷,但您可以使用 instanceOf :
Object obj = items.get("item");
if( obj instanceOf JSONArray )
{
//treat as JSONArray
}
else if(obj instanceOf JSONObject)
{
//treat as JSONObject
}