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 protected] request=login projectid=as
We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
stringify() method. “Stringification” is the process of converting a JavaScript object to a string.
In Java, you cannot change the class (the type) of an object. That is not possible. You may assign objects of different classes to the same variable. Naturally, you can only assign objects of the same class of the variable or any of its subclasses.
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.
Might not be so related to the issue above. However if you are looking for a way to serialize Java object as string, this could come in hand
package pt.iol.security; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.codec.binary.Base64; public class ObjectUtil { static final Base64 base64 = new Base64(); public static String serializeObjectToString(Object object) throws IOException { try ( ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream); ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);) { objectOutputStream.writeObject(object); objectOutputStream.flush(); return new String(base64.encode(arrayOutputStream.toByteArray())); } } public static Object deserializeObjectFromString(String objectString) throws IOException, ClassNotFoundException { try ( ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString)); GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream); ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream)) { return objectInputStream.readObject(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With