I receive an array of JSON objects, a part of which is below:
[{"Team":"LTD","Amount":10000.0,"Success":true},
{"Team":"XYZ","Amount":50000.0,"Success":false}]
I want to forcefully read all fields as string so as to make further processing easy and uniform. So Amount
must be read as 10000.0
and not as 1.0E5
.
Below is the code snippet that I use :
String input=IOUtils.toString(inputStream);
String[] fields="Amount|Success".split("\\|");
ReadContext inputParsed =JsonPath.parse(input);
List<JSONArray> resultList=Arrays.stream(fields)
.map(x -> inputParsed.read("$[*]."+x,JSONArray.class))
.collect(Collectors.toList());
//Further code to process resultList
When I print the values and type of Amount
from resultList
, they are shown as 1.0E5
and String
respectively. In between parsing and reading, the conversion from Double
to String
seems to occur in unexpected way.
I read a similar post here and it addresses a bit different problem.
The inputStream
and fields
, which are to be extracted , will be provided at run time. Hence, using POJO and other methods that need to define Class won't work.