问题
I have a huge JSON string as a response to a rest call. Part of the response has the following structure. I am using com.fasterxml.jackson
“historicalData”: {
“1585573790000”: {“score”:23.54, “count”:3},
“1585487390000”: {“score”:12.65, “count”:2}
},
//1585573790000 -> being the epoch time
The model I thought of so far is ArrayList of
private class HistoricalData {
Long epochTime;
Double score;
Longcount;
}
But I am unable to map the epoch time.
回答1:
your class HistoricalData
does not match the JSON structure.
You could use a Map
to match the JSON.
Map<Long, HistoryData> historicalData;
Then the class HistoryData
would look like this:
class HistoryData {
private Double score;
private Long count;
// getters & setters
}
Then you could process the map like:
historicalData.entrySet().forEach(entry -> {
// do something with the entry
// entry.getKey() would return the epochTime
// entry.getValue() would return the HistoryData object
});
But to be honest it would be better if you could change the JSON structure to:
"historicalData": [
{ "epochTime": "1585573790000", “score”:23.54, “count”:3},
{ "epochTime": "1585487390000", “score”:12.65, “count”:2}
]
Then you could use a List
of the class HistoricalData
:
private List<HistoricalData> historicalData;
And the class would look like:
public class HistoricalData {
private Long epochTime;
private Double score;
private Long count;
// getters & setters
}
来源:https://stackoverflow.com/questions/60950529/parse-json-string-using-jaxson-root-value-customization