I Have a JSON text format. So I put it into object first then to the Map, which has another map in the values (JSON has inner and outer map type in their text format). In the inner map printing a JSONDouble with two decimals isn’t a problem (I can just use printf("%.2f"))
but when JSONArray appears I can’t do that.
I have tried printf
function but it doesn’t work.
If it’s possible don’t use streams.
here is the code:
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"));
}
and output:
{"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]
2
Answers
Thank you guys for your time. This is solution to my problem:
So you have done the right this. All you need is to get all values from
JSONArray
, convert them separately and build an array ofString
.Finally you snippet could look like this: