有GET一个请求,在所有的阴谋之后,我得到了所有信息的一条大线。我怎样才能将它转换成更结构化的东西,比如hasmapor arrayList?
package ghibli;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class Main {
public static void main(String[] args) {
try {
String url = "https://ghibliapi.herokuapp.com/films";
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
// response.append("\n");
}
in.close();
// System.out.println(response.toString().replaceAll("^.|.$", ""));
String stringValuses = response.toString().replaceAll("^.|.$", "");
System.out.println(stringValuses);
Object objJson = JSONValue.parse(stringValuses);
// System.out.println(stringValuses);
JSONObject jsonObject = (JSONObject) objJson;
System.out.println(jsonObject);
String name = (String) jsonObject.get("title");
// System.out.println(name);
// Ghibli ghibli = new Ghibli();
// ghibli.startApp();
} catch (IOException ex) {
Logger.getLogger(Ghibli.class.getName()).log(Level.SEVERE, null, ex);
}
// ghibli.getData("https://ghibliapi.herokuapp.com/films");
}
}
我们使用 okhttp、gson、lombok,这将简化一切。我们得到这样的东西: