[英]How to get a String value from a Map <String, String[] > in Java?
Basically, I have a Map<String, String[] >
which contains a bunch of strings with a key. 基本上,我有一个
Map<String, String[] >
,其中包含一串带有键的字符串。 If I do String value = myMap.get("keyName");
如果我做
String value = myMap.get("keyName");
, this returns an Object
rather than a string, and echoing it produces something like this: Ljava.lang.String;@1dfa166
. ,这将返回一个
Object
而不是一个字符串,并对其进行回显会生成如下内容: Ljava.lang.String;@1dfa166
。 Doing toString()
doesn't help either. 进行
toString()
也无济于事。
What do I need to do to get the value as string: 我需要做什么才能将值作为字符串获取:
My code looks like this: 我的代码如下所示:
String value ="" + request().body().asFormUrlEncoded().get("keyName");
Here the asFormUrlEncoded()
method is what returns the Map
此处的
asFormUrlEncoded()
方法将返回Map
You're getting back an array of strings (second parameter to the generic Map as declared). 您将获得一个字符串数组(已声明的通用Map的第二个参数)。
Change it to 更改为
String [] values = myMap.get( "keyName" );
and check values.length to see how many strings are in the array. 并检查values.length以查看数组中有多少个字符串。 If it's just one, you can just access it as
values[0]
. 如果只是一个,则可以将其作为
values[0]
访问。
The reason it allows for a string array is because each key in the form could have multiple values, so it can't return a single string. 之所以允许使用字符串数组,是因为表单中的每个键都可以具有多个值,因此它不能返回单个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.