将Map值转换为String [英] Convert a Map value to String
本文介绍了将Map值转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将映射值转换为String
.
我尝试了toString()
方法,但它仍然返回Object
而不是String
I am trying to convert a map value to String
.
I tried toString()
method but it still returns an Object
instead of String
response = WS.sendRequest(findTestObject('api/test/TD-4_01_01-Valid'))
Map parsed = response.getHeaderFields()
String messageId = parsed.get('x-message-id').toString();
println messageId
实际输出:
[C5yZC5hcy5sb2NhbC0xMjgyNi05MzE1LTE=]
预期输出:
C5yZC5hcy5sb2NhbC0xMjgyNi05MzE1LTE=
推荐答案
ResponseObject#getHeaderFields将Map
键的Map
键返回到String
对象的List
键.您只需要获取键x-message-id
的String
对象的List
,并且由于您希望它返回单个结果,因此可以找到任何结果.
ResponseObject#getHeaderFields returns a Map
of String
keys to a List
of String
objects as vales. You simply need to get the List
of String
objects for the key x-message-id
and since you expect it to return a single result, find any.
ResponseObject response = WS.sendRequest(findTestObject('api/test/TD-4_01_01-Valid'));
Map<String, List<String>> parsed = response.getHeaderFields();
List<String> messageIdList = parsed.get("x-message-id");
String messageId = messageIdList.stream().findAny().orElseThrow(IllegalStateException::new);
这篇关于将Map值转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文