0

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"
}
Nithin
  • 748
  • 1
  • 10
  • 27
GregMa
  • 740
  • 2
  • 10
  • 25
  • you could simply add `mList` to `asMap` with key "executedTests", however it needs to adapt a little `Map` to accept the type. You then encode `asMap` and done. – Kaddath Feb 01 '18 at 14:29
  • Take a look to JSONObject:https://stackoverflow.com/questions/20117148/how-to-create-json-object-using-string – Monsif Mabrouk Feb 01 '18 at 14:53
  • @MonsifMabrouk - We are already using GSon, I'd like to avoid yet another json API if possible. – GregMa Feb 01 '18 at 15:55
  • @Kaddath - I've tried that, but wasn't able to. I've edited my OP to reflect what I tried and the effects. – GregMa Feb 01 '18 at 16:08
  • @GregMa it's in my first comment: you declare a map of strings `Map`, try to declare your map with something like `Map` instead – Kaddath Feb 01 '18 at 17:05
  • @Kaddath - You are saying to change the asMap declaration from String, String to String, Object? – GregMa Feb 01 '18 at 21:58
  • How are you taking json inputs ?? By file or by any response message ?? – Nithin Feb 02 '18 at 04:00

1 Answers1

1

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.

Steve Brandli
  • 556
  • 4
  • 14