繁体   English   中英

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

[英]Merge two java arrays to a single json string

我正在根据代码中的信息创建两个不同的数组。 我需要将两者合并(合并)并产生一个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"
}

在您的问题和对问题的回答中,似乎是您自己构建这些结构并可以更改它们的定义。 正如注释中所指出的那样,问题在于您的顶层结构(即HashMap)的值类型为String。 由于要添加列表作为值,因此必须将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);

或如果您使用上面的asMap则用asMap替代topMap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM