我正在处理 KhanAcademy 提供的一个相当旧的 API。
链接到 git:https://github.com/Khan/khan-api/wiki/Khan-Academy-API
我没有任何使用 REST 服务或如何从中解析 Json 的经验。我已经尝试过我可以在网上找到的内容,但大多数 SO 帖子或互联网上的其他内容并不涉及同时处理 REST 和 json。 我尝试将 json 映射到 map ,但由于我的 Json 查询未正确处理,因此无法正常工作。
以下是我尝试使用的一些代码:
public static Object getConnection(String url){
URL jsonUrl;
String message;
try{
jsonUrl = new URL(url);
System.out.println("This is the URL: "+ jsonUrl);
} catch (MalformedURLException ex) {
message = "failed to open a new conenction. "+ ex.getMessage();
//logger.warn(message);
throw new RuntimeException();
}
URLConnection connection;
try{
connection = jsonUrl.openConnection();
connection.connect();
}catch(IOException e){
message = "Failed to open a new connection. " + e.getMessage();
//logger.warn(message);
throw new RuntimeException();
}
Object jsonContents;
try{
jsonContents = connection.getContent();
System.out.println("This is the content: "+jsonContents);
}catch(IOException e){
message = "failed to get contents.";
//logger.warn(message);
throw new RuntimeException(message);
}
return jsonContents;
}
下面是使用 JAX RS API
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://www.khanacademy.org/api/v1/topictree");
JsonArray response = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class);
}
下面是一些“僵尸代码”,它是我尝试过的东西的汇编,主要是为了验证我是否真的迷路了,并且我已经寻找解决方案大约7个小时了?
JsonReader reader = new JsonReader(response);
JsonParser parser = new JsonParser();
JsonElement rootElement = parser.parse(reader);
JsonElement rootElement = parser.parse(response.getAsString());
JsonArray jsonArray = rootElement.getAsJsonArray();
ArrayList results = new ArrayList();
Gson myGson = new Gson();
for(JsonElement resElement : jsonArray){
//String mp4 = myGson.fromJson(resElement, );
}
JsonArray jarray =jsonObject.getAsJsonArray();
jsonObject= jarray.get(0).getAsJsonObject();
String result = jsonObject.get("title").getAsString();
System.out.println(result);
JsonObject resultObject = jsonObject.getAsJsonObject("url");
String result = resultObject.getAsString();
System.out.println(result);
JsonObject jsonObject=response.get(0).getAsJsonObject();
return new Gson().fromJson(url, mapType);
}
感谢任何帮助。
最佳答案
您可以使用Feign去做这件事。
我建议您创建 Java 类来表示 JSON 结构,其定义为 here
这是一个基本演示:
public class App {
public static void main(String[] args) {
KhanAcademyAPI khanAcademyAPI = Feign.builder()
.decoder(new GsonDecoder())
.logLevel(Logger.Level.HEADERS)
.target(KhanAcademyAPI.class, "http://www.khanacademy.org");
Topic root = khanAcademyAPI.tree();
root.children.forEach(topic1 -> System.out.println(topic1.slug));
Topic science = khanAcademyAPI.topic("science");
science.children.forEach(topic1 -> System.out.println(topic1.description));
}
public static class Topic {
String description;
boolean hide;
String slug;
List<Topic> children;
}
interface KhanAcademyAPI {
@RequestLine("GET /api/v1/topictree")
Topic tree();
@RequestLine("GET /api/v1/topic/{topic_slug}")
Topic topic(@Param("topic_slug") String slug);
}
}
我只使用了以下 Maven 依赖项:
- io.github.openfeign:feign-core:10.2.0
- io.github.openfeign:feign-gson:10.2.0
关于java - 从 RESTful API 中提取数据以解析为 json 并从 JAVA 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485049/