这个例外集团所有下一次治疗,我不明白为什么? 并且json文件没有[20]那些元素
package com.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sun.security.krb5.internal.PAData;
import com.dao.PaysDao;
import com.dao.impl.PaysDaoImpl;
import com.dto.Pays;
public class ParserPays {
public static void main(String[] args) {
// TODO Auto-generated method stub
int c=65;
System.out.println((char) c);
try {
PaysDao payDao = new PaysDaoImpl();
Pays pa = new Pays();
for (int j =65 ;j<=91;j++){
JSONObject json = new JSONObject(readUrl("http://autocomplete.wunderground.com/aq?query=" + (char)j));
JSONArray result = json.getJSONArray("RESULTS");
for(int i = 1 ; i <= result.length();i++){
JSONObject row = result.getJSONObject(i);
String type =(String) row.get("type");
String iso =(String) row.get("c");
String city =(String) row.get("city");
System.out.println(type);
System.out.println(iso);
System.out.println(city);
if (type.equals("country")){
pa.setCodeiso(iso);
pa.setIdpays(0);
pa.setLibilepays(city);
pa.setVilles(null);
payDao.saveOrUpdate(pa);
}
else System.out.println("not a country");
}
for(int k = 65 ; k <= 91 ; k++){
JSONObject json1 = new JSONObject(readUrl("http://autocomplete.wunderground.com/aq?query="+(char)j + (char)k));
JSONArray result1 = json1.getJSONArray("RESULTS");
for(int i = 0 ; i <= result1.length();i++){
JSONObject row = result1.getJSONObject(i);
String type =(String) row.get("type");
String iso =(String) row.get("c");
String city =(String) row.get("city");
System.out.println(type);
System.out.println(iso);
System.out.println(city);
if (type.equals("country")){
pa.setCodeiso(iso);
pa.setIdpays(0);
pa.setLibilepays(city);
pa.setVilles(null);
payDao.saveOrUpdate(pa);
}
else System.out.println("not a country");
}
for(int h = 65 ; h<= 91 ; h++){
JSONObject json2 = new JSONObject(readUrl("http://autocomplete.wunderground.com/aq?query=" + (char)j + (char)k + (char)h));
JSONArray result2 = json2.getJSONArray("RESULTS");
for(int i = 0 ; i <= result2.length();i++){
JSONObject row = result2.getJSONObject(i);
String type =(String) row.get("type");
String iso =(String) row.get("c");
String city =(String) row.get("city");
System.out.println(type);
System.out.println(iso);
System.out.println(city);
if (type.equals("country")){
pa.setCodeiso(iso);
pa.setIdpays(0);
pa.setLibilepays(city);
pa.setVilles(null);
payDao.saveOrUpdate(pa);
}
else System.out.println("not a country");
}
}
}
}
/* while (i.hasNext()) {
System.out.println(i.next());
// Here I try to take the title element from my slide but it doesn't work!
String title = (String) jsonObject.get("title");
System.out.println(title);
}*/
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String readUrl(String string) throws IOException {
BufferedReader reader = null;
try {
String urlString = string;
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
}
这是控制台错误:
A
city
DE
Aalen
not a country
city
BE
Aarschot
not a country
city
NL
Aalsmeer
not a country
city
NL
Aarle
not a country
city
NL
Aalten
not a country
city
BE
Aalter
not a country
city
CH
Aarau
not a country
city
BE
Aartselaar
not a country
city
NL
Aalburg
not a country
city
CH
Aadorf
not a country
city
CH
Aarburg
not a country
city
CH
Aarwangen
not a country
city
CH
Aarberg
not a country
city
GL
Aasiaat
not a country
city
PF
AAA
not a country
city
AU
AAB
not a country
city
EG
AAC
not a country
city
DE
Aach
not a country
city
DE
Aachen
not a country
city
BE
Aalst
not a country
city
DE
Aalen
not a country
city
BE
Aarschot
not a country
city
NL
Aalsmeer
not a country
city
NL
Aarle
not a country
city
NL
Aalten
not a country
city
BE
Aalter
not a country
city
CH
Aarau
not a country
city
BE
Aartselaar
not a country
city
NL
Aalburg
not a country
city
CH
Aadorf
not a country
city
CH
Aarburg
not a country
city
CH
Aarwangen
not a country
city
CH
Aarberg
not a country
city
GL
Aasiaat
not a country
city
PF
AAA
not a country
city
AU
AAB
not a country
city
EG
AAC
not a country
city
DE
Aach
not a country
city
DE
Aachen
not a country
org.json.JSONException: JSONArray[20] not found.
at org.json.JSONArray.get(JSONArray.java:191)
at org.json.JSONArray.getJSONObject(JSONArray.java:287)
at com.parser.ParserPays.main(ParserPays.java:59)
答案 0 :(得分:3)
for(int i = 1 ; i <= result.length();i++)
没有。数组索引从0开始,以len - 1结束。这应该是:
for (int i = 0; i < result.length(); i++)