嗨,我只想用java(+spring boot)以最佳方式解析get response中的1个值。
{ "table": "A", "currency": "usd", "code": "USD", "rates": [ { "no": "073/A/NBP/2021", "effectiveDate": "2021-04-16", "mid": 3.7978 } ]}
{
"table": "A",
"currency": "usd",
"code": "USD",
"rates": [
"no": "073/A/NBP/2021",
"effectiveDate": "2021-04-16",
"mid": 3.7978
}
]
我正在寻找一种方法来解析“mid”值,而不为此响应创建dto。在最坏的情况下,我只做一个子串。
blmhpbnm1#
试试这个。
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);
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);
输出:
3.7978
1条答案
按热度按时间blmhpbnm1#
试试这个。
输出: