简体   繁体   English

在 Groovy 上使用 JSON 响应并使用响应填充 Map

[英]Working with JSON Response on Groovy and populating a Map with the response

I'm making a POST request to a URL with groovy (using HttpResponse - Unirest) and retrieving the response as a JSONObject.我正在使用 groovy(使用 HttpResponse - Unirest)向 URL 发出 POST 请求,并将响应作为 JSONObject 检索。

Next, I'm creating a Map with that response, using:接下来,我使用该响应创建一个 Map,使用:

Map<String, Object> myMap = new HashMap<>(response.toMap())

My JSON response is something like this (sorry for looking like a weird response, but I had to cut the sensible information):我的 JSON 响应是这样的(对不起,看起来很奇怪,但我不得不删掉明智的信息):

{
    "responses": [
        {
            "took": 123,
            "timed_out": true,
            },
            "hits": {
                "total": 123,
                "max_score": null,
                "hits": [
                    {
                        "_source": {
                            "message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum payloadType=JSONpayload=[{"some json payload here"}]
                }
            }
        ]
    ]
}

} }

I have two questions regarding this:我对此有两个问题:

1 - When I try to print the key and the values, the key is "responses" and all the other parts are values. 1 - 当我尝试打印键和值时,键是“响应”,所有其他部分都是值。 Is this normal?这是正常的吗? I also can't use a put function to add values to map.我也不能使用 put function 将值添加到 map。 Which means, my map is kinda funky at the moment and I can't seem to find a solution to work with this.这意味着,我的 map 目前有点时髦,我似乎找不到解决方案。 Should I use another library or so?我应该使用另一个库吗? I don't mind using another one, I just need to find a way to have a map that I can work with.我不介意使用另一个,我只需要找到一种方法来拥有我可以使用的 map。

2 - I need my map values to be trimmed to only present results after a specific String, in this case, I need my map to be populated with the JSON payload that comes after "payloadType=JSONpayload=" to store it in a variable and then work with that information. 2 - 我需要将我的 map 值修剪为仅在特定字符串之后显示结果,在这种情况下,我需要使用 Z0ECD11C1D7A287401D1D7A287401D1D7A287401D1D7A287401D1D7A287401D1D7A287401D148A23 和 JSONpayload 将其存储在“BBD7A2F8Z 有效负载”之后然后使用该信息。 How can I achieve that?我怎样才能做到这一点? Is a regex enough to make it?正则表达式是否足以实现它? I was thinking of using an iterator to iterate through all the values and then trim them to a variable.我正在考虑使用迭代器来遍历所有值,然后将它们修剪为变量。 Is this a good solution for this case?对于这种情况,这是一个很好的解决方案吗?

Sorry if something is confusing, but I'm still a newbie at Java and Groovy and it's my first assignment at the project.抱歉,如果有什么令人困惑的地方,但我仍然是 Java 和 Groovy 的新手,这是我在项目中的第一个任务。

Thanks!谢谢!

You can map your response not to Map, but to your custom created object called POJO.您可以 map 您的响应不是对 Map,而是对您自定义创建的 object 的响应,称为 POJO。 Next, with library Jackon, or Gson parse this Json.接下来,使用库 Jackon 或 Gson 解析此 Json。 In the end you can have access to each value in Json, and for exmaple using regexp, extract certain data from specific fields.最后,您可以访问 Json 中的每个值,例如使用正则表达式,从特定字段中提取某些数据。 This approach is long term approach, easy to tests and maintanance.这种方法是长期的方法,易于测试和维护。

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

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