abi1964 abi1964 - 6 days ago 6
Java Question

how to convert object to string in java

I have a function that returns map value (String) as a generic Object. How do I convert it back to string. I tried toString() but all i get is

end[Ljava.lang.String;@ff2413


public Object getParameterValue(String key)
{
Iterator iterator=params.entrySet().iterator();

while(iterator.hasNext())
{
Map.Entry me=(Map.Entry)iterator.next();
String[] arr=(String[])me.getValue();
log.info(me.getKey().toString()+"="+arr[0]);
}
if(params.containsKey(key))
{
log.info(key+"="+params.get(key));
return params.get(key);
}


return null;
}


Receiving end

String temp=data.getParameterValue("request").toString();
log.info("end"+temp);


log.info(me.getKey().toString()+"="+arr[0]);
give me an output


email=x@as.com
request=login
projectid=as

Answer

I'm afraid your map contains something other than String objects. If you call toString() on a String object, you obtain the string itself.

What you get [Ljava.lang.String indicates you might have a String array.