我正在试图找到一种方法来将编码从读取文件更改为URL。这是代码的开始,但我尝试的所有内容都以语法错误结束。有什么建议?
public static void main(String[] args){
readJson("jsonfilejson.txt");
}
public static void readJson(String file) {
Airline airLine = null;
ArrayList<Airline> list = new ArrayList<Airline>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
String data = "";
while((line=br.readLine())!=null){
data += line;
}
ArrayList<String> flightsList = new ArrayList<String>();
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject values = jsonArray.getJSONObject(i);
JSONObject nameObject = values.getJSONObject("firstAirline");
String name = nameObject.get("name").toString();
JSONArray segmentArray = values.getJSONArray("segments");
for (int j = 0; j < segmentArray.length(); j++) {
JSONObject segments = segmentArray.getJSONObject(j);
flightsList.add(segments.getString("flightNumber"));
}
String stops = values.get("stops").toString();
JSONObject priceObject = values.getJSONObject("price");
double price = Double.parseDouble(priceObject.get("amount").toString());
airLine = new Airline(name, price, flightsList, Integer.parseInt(stops));
list.add(airLine);
flightsList.clear();
}
}
答案 0 :(得分:1)
你似乎走在了正确的轨道上。尝试将您的字符串转换为您可以从中读取流的网址
URL fileUrl = new URL(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fileUrl.openStream()));
希望有所帮助