提问人:RikiSrakatan 提问时间:11/17/2023 更新时间:11/17/2023 访问量:31
如何获取某些 Json 变量 [closed]
How can I get certain Json variable [closed]
问:
我正在编写我的第一个 java 项目,货币转换器,但我无法从 json 文件中获取某些变量以将其添加到我的函数中
public class Main {
public static void converter() {
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double d = Double.parseDouble(t1.getText());
double d1 = (d/Json_Reader.rateUSD);
String str1 = String.valueOf(df.format(d1));
t2.setText(str1);
}
});
b2.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
double d2 = Double.parseDouble(t2.getText());
double d3 = (d2 * Json_Reader.rateUSD);
String str2 = String.valueOf(df.format(d3));
t1.setText(str2);
}
});
public static void main(String[] args) throws IOException, URISyntaxException {
converter();
Json_Reader.UrlToRates();
}
}
我删除了 Main 中的大部分 AWT 代码
public class Json_Reader {
static double rateUSD;
public static void UrlToRates() throws IOException, URISyntaxException, JSONException {
String sURL = "https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json"; //just a string
URL url = new URI(sURL).toURL(); //converting URI to URL
URLConnection request = url.openConnection();
request.connect(); //openning connection
Gson gson = new Gson(); //creating Gson object
BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) request.getContent())); //geting input as StringBuilder
StringBuilder sb = new StringBuilder();
String line;
JsonElement jelem = gson.fromJson(br, JsonElement.class); //converting StringBuilder to JsonElement
while ((line = br.readLine()) != null) { //converting StringBuilder to String
sb.append(line);}
JsonObject jo = jelem.getAsJsonObject(); //creating JsonObject
String req_result = jo.get("result").getAsString(); //geting result as a String
System.out.println(req_result);
rateUSD = JsonPath.using(Configuration.defaultConfiguration()).parse(jo).read("$..rate[1]", Double.class); //geting certian rate
}
}
主要问题是 b1 和 b2 区块中的双倍利率美元,任何输出都将是无穷大的
我认为问题是执行顺序,但我不确定如何解决它
答: 暂无答案
评论