简体   繁体   English

如何仅解析 JSON HTTP 响应 Spring 引导中的 1 个变量

[英]How to parse only 1 variable from JSON HTTP response Spring Boot

Hi I would like to parse only 1 value from GET response in the most optimal way using Java (+Spring Boot).嗨,我想使用 Java(+Spring Boot)以最佳方式从 GET 响应中解析 1 个值。

{
    "table": "A",
    "currency": "usd",
    "code": "USD",
    "rates": [
        {
            "no": "073/A/NBP/2021",
            "effectiveDate": "2021-04-16",
            "mid": 3.7978
        }
    ]
}

Im looking for way to parse "mid" value without creating DTO for this response.我正在寻找在不为此响应创建 DTO 的情况下解析“中间”值的方法。 In the worst case I will do just a substring.在最坏的情况下,我只会做一个 substring。

Try this.尝试这个。

String input = "{\r\n"
    + "    \"table\": \"A\",\r\n"
    + "    \"currency\": \"usd\",\r\n"
    + "    \"code\": \"USD\",\r\n"
    + "    \"rates\": [\r\n"
    + "        {\r\n"
    + "            \"no\": \"073/A/NBP/2021\",\r\n"
    + "            \"effectiveDate\": \"2021-04-16\",\r\n"
    + "            \"mid\": 3.7978\r\n"
    + "        }\r\n"
    + "    ]\r\n"
    + "}";
String midValue = input.replaceFirst("(?s).*\"mid\"\\s*:\\s*([-.\\d]+).*", "$1");
System.out.println(midValue);

output: output:

3.7978

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

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