我有两个网址(urlA和urlB),并且这两个网址都以相同的json格式返回给我json响应。下面是我的JSON字符串,我通过从urlA调用回来。为了理解目的,我只有三个reportRecords
缩短了它。一般来说,它可能超过~500 reportRecords
{
"aggRecords": {
"reportRecords": [
{
"min": 0,
"max": 12,
"avg": 0.3699187,
"count": 246,
"sumSq": 571,
"stddev": 1.4779372,
"median": 0,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "PostProcessing",
"Type": "PostProcessing"
},
"value": 91
},
{
"min": 0,
"max": 23,
"avg": 2.3991289E-4,
"count": 1463031,
"sumSq": 3071,
"stddev": 0.045814946,
"median": 0,
"percentileMap": {
"95": 0
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "ResourceContext",
"Type": "ResourceContext"
},
"value": 351
},
{
"min": 0,
"max": 1209,
"avg": 1.9203402,
"count": 7344636,
"sumSq": 71832774,
"stddev": 2.4683187,
"median": 2,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "Client::Sync",
"Type": "Client::Sync"
},
"value": 14104200
}
]
}
}
同样地,我也通过调用urlB
来获取另一个JSON响应,它与上面显示的json格式完全相同。现在使用下面的代码,我能够序列化来自urlA
对象中的DataTransactionMetrics
的json响应。
public class JSONParser {
private static RestTemplate restTemplate = new RestTemplate();
private static final Gson gson = new Gson();
private static final String URLA = "urlA";
private static final String URLB = "urlB";
public static void main(String[] args) {
String jsonTransactionLine = restTemplate.getForObject(URLA, String.class);
System.out.println(jsonTransactionLine);
JsonElement jelement = new JsonParser().parse(jsonTransactionLine);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("aggRecords");
JsonArray jarray = jobject.getAsJsonArray("reportRecords");
Type type = new TypeToken<List<DataTransactionMetrics>>() {
}.getType();
List<DataTransactionMetrics> serializeRecord = gson.fromJson(jarray, type);
// how can I serialize json data coming from URLB to serializeRecord object as well?
}
}
以下是我的DataTransactionMetrics
课程 -
public class DataTransactionMetrics {
private String metricName;
private Map<String, Integer> percentileMap;
private String median;
private String stddev;
private String sumSq;
private String count;
private String avg;
private String max;
private String min;
// getters here
public Dimensions dimensions;
class Dimensions {
private String env;
private String pool;
@SerializedName("Name")
private String name;
// getters here
}
}
问题陈述: -
如何在同一个serializeRecord
对象中序列化来自urlB的json响应,以便来自url(urlA和urlB)的所有数据都存在于同一个serializeRecord
对象中?这可能吗?
答案 0 :(得分:1)
也可以对urlB
执行相同操作并连接两个结果列表:
static final Type listOfMetricsType = new TypeToken<List<DataTransactionMetrics>>() {}.getType();
private List<DataTransactionMetrics> loadMetrics(String url) {
String jsonString = restTemplate.getForObject(URLA, String.class);
JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject();
JsonArray jarr = json.getAsJsonObject("aggRecords").getAsJsonArray("reportRecords");
return gson.fromJson(jarr, listOfMetrics);
}
public static void main(String[] args) {
List<DataTransactionMetrics> metrics = loadMetrics(URLA);
metrics.addAll(loadMetrics(URLB));
// metrics now contains all metrics (from URLA and URLB)
}