【发布时间】:2019-09-18 00:28:38
【问题描述】:
我正在尝试从外部 API 获取 JSON 并对其进行解析,以便我可以访问 JSON 对象值以避免我不需要的值(字段)。
我对 JSONOBject 库进行了一些研究并解析了对象,但是,感觉我编写了太多的硬编码并且根本没有正确执行,非常感谢反馈。
用户输入后的输出结果:
发送'GET'请求到URL:https://api.coindesk.com/v1/bpi/currentprice/(user输入货币)
在 UTC 时间/日期获取的数据:2019 年 9 月 17 日 22:51:00 UTC 说明:乌克兰格里夫纳 浮动利率:253737.6633
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class App {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the currency you would like to get BTC current rate for");
String userInput = in.readLine();
try {
String url = "https://api.coindesk.com/v1/bpi/currentprice/" + userInput;
URL obj = new URL(url);
// HttpURLConnection instance is making a request
//openConnection() method of URL class opens the connection to specified URL
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// saving the response code from the request
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("---------------------------------------------");
System.out.println("Response Code from the HTTP request : " + responseCode);
System.out.println("---------------------------------------------");
//creating reader instance to reade the response from the HTTP GET request
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String userInputLine;
StringBuilder response = new StringBuilder();
//if there is still something to read from -> then read and append to String builder -> can be mutable
while ((userInputLine = reader.readLine()) != null) {
response.append(userInputLine);
}
//closing reader
reader.close();
//Converting response to JSON object
JSONObject myresponse = new JSONObject(response.toString());
JSONObject bpiObject = new JSONObject(myresponse.getJSONObject("bpi").toString());
JSONObject currencyObjects = new JSONObject(bpiObject.getJSONObject(userInput).toString());
// since response got a objects within objects, we need to dig dipper in order to access field values
JSONObject timeObject = new JSONObject(myresponse.getJSONObject("time").toString());
System.out.println("Data fetched at UTC time/date : " + timeObject.getString("updated"));
System.out.println("Description : " + currencyObjects.getString("description") + "\nRate float : " + currencyObjects.getString("rate_float"));
} catch (Exception e) {
System.out.println(e);
}
}
}
【问题讨论】:
-
您可以使用
ObjectMapper将json字符串转换为Java对象进行操作。 -
@LHCHIN 嘿,谢谢,我知道我可以使用字段(对象)创建一个 POJO 类,但是之后我将如何挖掘这些 JSON 对象 -> 例如,如果有 JSON 对象 {time } 和时间有一个名为“更新”的字段我如何访问该字段。