Reputation: 740
I am creating two different arrays from information in code. I need to concatenate(merge) those two into one and produce a json object to pass on to a different method. I would prefer to use gson because I am using those
I am using gson, and have tried so many different methods, I am completely lost. Any help?
NOTE: I've tried adding mList to asMap,
asMap.putAll(mList);
but get
putAll (java.util.Map<? extends java.lang.String,? extends java.lang.string>) in Map cannot be applied to (java.util.List <java.util.Map<java.lang.String,java.lang.String>>)
I have one array:
Map<String, String> asMap = new HashMap<>();
Which looks like:
{
"batchId":"25248e931e5948b990f528d26ee6a9f5",
"almServer":"server.com",
"almDomain":"EBUSINESS",
"almProject":"STERLING",
"testSetUrl":"http://test.com",
"testParameters":"{\"browser\":\"chrome\",\"browser-version\":\"56\",\"language\":\"english\",\"country\":\"USA\"}",
"startTime":"01/20/2018 06:37:05 PM",
"finishTime":"01/21/2018 05:30:25 AM",
"totalDuration":"10h 53m 19s 922ms",
"passed":159,
"testsExecuted":214,
"failed":52,
"notCompleted":3,
"testPassPercentage":"75.4"
}
A second:
List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
Which looks like:
[
{
"runDuration":"9m 33s 642ms",
"testId":"12100",
"automatedTest":"CancelFullOrder",
"batchItemPosition":"1",
"runPosition":"1",
"executionDate":"01/21/2018",
"executionTime":"02:48:50 AM",
"runId":"69257",
"status":"PASSED"
},
{
"runDuration":"8m 56s 991ms",
"testId":"12101",
"automatedTest":"CancelOrderPartially",
"batchItemPosition":"2",
"runPosition":"1",
"executionDate":"01/21/2018",
"executionTime":"02:49:30 AM",
"runId":"69258",
"status":"PASSED"
}
]
I want to combine them into one jsonobject that would look like:
{
"batchId":"25248e931e5948b990f528d26ee6a9f5",
"almServer":"server.com",
"almDomain":"EBUSINESS",
"almProject":"STERLING",
"testSetUrl":"http://test.com",
"testParameters":"{\"browser\":\"chrome\",\"browser-version\":\"56\",\"language\":\"english\",\"country\":\"USA\"}",
"executedTests":[
{
"runDuration":"9m 33s 642ms",
"testId":"12100",
"automatedTest":"CancelFullOrder",
"batchItemPosition":"1",
"runPosition":"1",
"executionDate":"01/21/2018",
"executionTime":"02:48:50 AM",
"runId":"69257",
"status":"PASSED"
},
{
"runDuration":"8m 56s 991ms",
"testId":"12101",
"automatedTest":"CancelOrderPartially",
"batchItemPosition":"2",
"runPosition":"1",
"executionDate":"01/21/2018",
"executionTime":"02:49:30 AM",
"runId":"69258",
"status":"PASSED"
}
],
"startTime":"01/20/2018 06:37:05 PM",
"finishTime":"01/21/2018 05:30:25 AM",
"totalDuration":"10h 53m 19s 922ms",
"passed":159,
"testsExecuted":214,
"failed":52,
"notCompleted":3,
"testPassPercentage":"75.4"
}
Upvotes: 0
Views: 277
Reputation: 566
It appears from your question and your answer to the questions in the comments that you are building these structures yourself and can change the definitions of them. The problem, as has been pointed out in the comments, is that your top level structure, which is the HashMap, has values of type String. Since you want to add a List as a value, you must generalize the type of the HashMap value to Object:
Map<String, Object> asMap = new HashMap<>();
If you are building the HashMap yourself, you should be able use this new definition when building the Map as you are already doing. But if you already have the Map<String, String>
, you can force the cast to Map<String, Object>
with:
@SuppressWarnings("unchecked")
Map<String, Object> topMap = (Map<String, Object>) (Map<String, ?>) asMap;
(Maybe there is a better way to do this. Java cannot assume that, just because String is a subclass of Object, it's okay to do this cast. Why it requires two casts to make this work is beyond my understanding.)
Then, to combine them:
asMap.put("executedTests", mList);
or substitute topMap
for asMap
if you used the cast above.
Upvotes: 1