简体   繁体   English

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

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

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. 我需要将两者合并(合并)并产生一个json对象,以传递给其他方法。 I would prefer to use gson because I am using those 我更喜欢使用gson,因为我正在使用那些

I am using gson, and have tried so many different methods, I am completely lost. 我正在使用gson,并且尝试了许多不同的方法,我完全迷失了。 Any help? 有什么帮助吗?

NOTE: I've tried adding mList to asMap, 注意:我尝试将mList添加到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: 我想将它们组合成一个看起来像这样的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"
}

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. 正如注释中所指出的那样,问题在于您的顶层结构(即HashMap)的值类型为String。 Since you want to add a List as a value, you must generalize the type of the HashMap value to Object: 由于要添加列表作为值,因此必须将HashMap值的类型概括为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. 如果您要自己构建HashMap,则在构建Map时应该已经可以使用此新定义。 But if you already have the Map<String, String> , you can force the cast to Map<String, Object> with: 但是,如果您已经有了Map<String, String> ,则可以使用以下方法强制将其强制转换为Map<String, Object>

@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.) (也许有一种更好的方法可以做到这一点。Java无法假设,因为String是Object的子类,所以可以进行这种强制转换。为什么需要两次强制转换才能完成这项工作,这超出了我的理解。)

Then, to combine them: 然后,将它们结合起来:

asMap.put("executedTests", mList);

or substitute topMap for asMap if you used the cast above. 或如果您使用上面的asMap则用asMap替代topMap

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

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