提问人:Davido 提问时间:11/11/2023 最后编辑:Davido 更新时间:11/12/2023 访问量:109
有没有办法在 JSONArray 中对双打进行四舍五入?
Is there a way to round doubles in JSONArray?
问:
我有一个JSON文本格式。因此,我首先将其放入对象中,然后放入 Map,该映射的值中有另一个映射(JSON 的文本格式具有内部和外部映射类型)。在内部映射中,打印带有两位小数的 JSONDouble 不是问题(我可以使用,但是当 JSONArray 出现时,我不能这样做。printf("%.2f"))
我试过功能,但它不起作用。printf
如果可能,请不要使用流。
代码如下:
package doplomski.rad.glavna;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.math.BigDecimal;
import java.net.URI;
import java.util.*;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Glavna {
public static void main(String[] args) throws ParseException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("API url"))
.header("X-RapidAPI-Host", "website url")
.header("X-RapidAPI-Key", "key")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = null;
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.out.println(response.body());
System.out.println();
Object obj = new JSONParser().parse(response.body());
Map<String, Map<String, BigDecimal>> output = (Map) obj;
System.out.printf("%.2f%n", output.get("outputs").get("ac_annual"));
System.out.printf("%.2f%n", output.get("outputs").get("solrad_annual"));
System.out.printf("%.2f%n", output.get("outputs").get("capacity_factor"));
System.out.println(output.get("outputs").get("ac_monthly"));
System.out.println(output.get("outputs").get("dc_monthly"));
System.out.println(output.get("outputs").get("poa_monthly"));
System.out.println(output.get("outputs").get("solrad_monthly"));
}
并输出:
{"inputs":{"azimuth":"180","system_capacity":"4","losses":"14","array_type":"1","module_type":"0","gcr":"0.4","dc_ac_ratio":"1.2","inv_eff":"96.0","radius":"0","dataset":"nsrdb","tilt":"10","address":"boulder, co","soiling":[12.0,4.0,45.0,23.0,9.0,99.0,67.0,12.54,54.0,9.0,0.0,7.6],"albedo":"0.3","bifaciality":"0.7"},"errors":[],"warnings":[],"version":"8.2.1","ssc_info":{"version":280,"build":"Linux 64 bit GNU/C++ Oct 18 2023 07:13:03","module":"pvwattsv8"},"station_info":{"lat":40.0099983215332,"lon":-105.2600021362305,"elev":1635.640014648438,"tz":-7.0,"location":"149190","city":"","state":"Colorado","country":"United States","solar_resource_file":"149190.csv","distance":1750,"weather_data_source":"NSRDB PSM V3 GOES tmy-2020 3.2.0"},"outputs":{"ac_monthly":[350.4450331552693,436.3879540968705,340.7495159069064,442.1920779147474,534.4142671193669,19.89441005557222,206.6577352851099,492.4427511771748,241.7735300594523,382.6085934358003,376.0863140563925,306.6293313390505],"poa_monthly":[104.9805590746965,131.3527386112043,104.2011355720695,138.6804496432297,177.348356252559,9.001239090243104,70.67352420990476,166.9411671806375,79.89308311196012,122.4161912004151,115.6650356864296,91.94797329425062],"solrad_monthly":[3.386469647570856,4.69116923611444,3.361326953937725,4.622681654774324,5.720914717824484,0.3000413030081034,2.279791103545315,5.385198941310887,2.663102770398671,3.948909393561777,3.855501189547655,2.966063654653246],"dc_monthly":[368.0682926594546,457.2045927636358,359.1841462238203,464.7078109487218,561.0990701509306,27.17952816676697,221.2817047541209,517.4117655437504,256.4903025712858,402.0961281476057,394.804951938705,322.3432439889111],"ac_annual":4130.281513601723,"solrad_annual":3.598430880520624,"capacity_factor":11.78733308676288}}
4130,28
3,60
11,79
[350.4450331552693,436.3879540968705,340.7495159069064,442.1920779147474,534.4142671193669,19.89441005557222,206.6577352851099,492.4427511771748,241.7735300594523,382.6085934358003,376.0863140563925,306.6293313390505]
[368.0682926594546,457.2045927636358,359.1841462238203,464.7078109487218,561.0990701509306,27.17952816676697,221.2817047541209,517.4117655437504,256.4903025712858,402.0961281476057,394.804951938705,322.3432439889111]
[104.9805590746965,131.3527386112043,104.2011355720695,138.6804496432297,177.348356252559,9.001239090243104,70.67352420990476,166.9411671806375,79.89308311196012,122.4161912004151,115.6650356864296,91.94797329425062]
[3.386469647570856,4.69116923611444,3.361326953937725,4.622681654774324,5.720914717824484,0.3000413030081034,2.279791103545315,5.385198941310887,2.663102770398671,3.948909393561777,3.855501189547655,2.966063654653246]
答:
0赞
Oleg Cherednik
11/12/2023
#1
所以你做对了。您只需要从 中获取所有值,分别转换它们并构建一个 .JSONArray
String
public static String toString(JSONArray jsonArray) {
return IntStream.rangeClosed(0, jsonArray.length())
.mapToObj(i -> String.format("%.2f", (BigDecimal) jsonArray.get(i)))
.collect(Collectors.joining(",", "[", "]"));
}
最后,您的代码片段可能如下所示:
System.out.printf("%.2f%n", output.get("outputs").get("ac_annual"));
System.out.printf("%.2f%n", output.get("outputs").get("solrad_annual"));
System.out.printf("%.2f%n", output.get("outputs").get("capacity_factor"));
System.out.println(toString(output.get("outputs").get("ac_monthly")));
System.out.println(toString(output.get("outputs").get("dc_monthly")));
System.out.println(toString(output.get("outputs").get("poa_monthly")));
System.out.println(toString(output.get("outputs").get("solrad_monthly")));
评论
0赞
Davido
11/12/2023
它说:无法解析“JSONArray”中的方法“length”
0赞
Oleg Cherednik
11/12/2023
你用什么?你能告诉我全名吗?JSONArray
0赞
Davido
11/12/2023
另外,当我添加 toString() 方法时,它说所需的类型是 JSONArray,而不是 Float
0赞
Oleg Cherednik
11/12/2023
当然,我可以在调试中看到 - 这是BigDecimal
0赞
Davido
11/12/2023
现在在这里添加了整个代码。不知道我在哪里可以看到类型od JSONarray
0赞
Davido
11/12/2023
#2
谢谢你们抽出时间接受采访。 这是我问题的解决方案:
HttpResponse<String> response = null;
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.out.println(response.body());
System.out.println();
Object obj = new JSONParser().parse(response.body());
Map<String, Map<String, Object>> output = (Map) obj;
System.out.printf("%.2f%n", ((Double) output.get("outputs").get("ac_annual")));
System.out.printf("%.2f%n", ((Double) output.get("outputs").get("solrad_annual")));
System.out.printf("%.2f%n", ((Double) output.get("outputs").get("capacity_factor")));
JSONArray acMonthlyArray = (JSONArray) output.get("outputs").get("ac_monthly");
System.out.println(toString(acMonthlyArray));
JSONArray dcMonthlyArray = (JSONArray) output.get("outputs").get("dc_monthly");
System.out.println(toString(dcMonthlyArray));
JSONArray poaMonthly = (JSONArray) output.get("outputs").get("poa_monthly");
System.out.println(toString(poaMonthly));
JSONArray solrad_monthly = (JSONArray) output.get("outputs").get("solrad_monthly");
System.out.println(toString(solrad_monthly));
}
public static String toString(JSONArray array) {
StringBuilder result = new StringBuilder("[");
for (int i = 0; i < array.size(); i++) {
BigDecimal value = new BigDecimal(array.get(i).toString());
result.append(String.format("%.2f", value));
if (i < array.size() - 1) {
result.append(", ");
}
}
result.append("]");
return result.toString();
}
}
评论