将两个java数组合并为一个json字符串

时间:2018-02-01 14:22:47

标签: java json gson

我正在从代码中的信息创建两个不同的数组。我需要将这两个连接(合并)为一个并生成一个json对象以传递给另一个方法。我更喜欢使用gson,因为我正在使用那些

我正在使用gson,尝试了很多不同的方法,我完全迷失了。有什么帮助吗?

注意:我已尝试将mList添加到asMap,

asMap.putAll(mList);

但是

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>>)

我有一个数组:

Map<String, String> asMap = new HashMap<>();

看起来像:

{
    "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"
}

第二个:

List<Map<String, String>> mList = new ArrayList<Map<String, String>>();

看起来像:

[
    {
        "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"
    }
]

我想将它们组合成一个看起来像的jsonobject:

{
    "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"
}

1 个答案:

答案 0 :(得分:1)

从您的问题和您对评论中的问题的回答中可以看出,您自己正在构建这些结构,并且可以更改它们的定义。正如评论中指出的那样,问题在于您的顶级结构(即HashMap)具有String类型的值。由于您要将List作为值添加,因此必须将HashMap值的类型概括为Object:

Map<String, Object> asMap = new HashMap<>();

如果您自己构建HashMap,则在构建Map时应该能够使用这个新定义。但是,如果您已经拥有Map<String, String>,则可以使用:

强制转换为Map<String, Object>
@SuppressWarnings("unchecked")
Map<String, Object> topMap = (Map<String, Object>) (Map<String, ?>) asMap;

(也许有一种更好的方法可以做到这一点.Java不能假设,因为String是Object的子类,它可以做这个演员。为什么它需要两个演员才能使这项工作超出我的理解。)

然后,将它们组合起来:

asMap.put("executedTests", mList);
如果你使用上面的演员表,请用{{1>}代替topMap