Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert object to string in java

Tags:

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 protected] request=login projectid=as 
like image 546
AabinGunz Avatar asked May 19 '11 07:05

AabinGunz


People also ask

How do I convert an object to a string in java?

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.

How do you change an object to text?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

What is the name of the method that converts an object to a string?

stringify() method. “Stringification” is the process of converting a JavaScript object to a string.

How do you change an object type in java?

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.


2 Answers

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.

like image 152
Vivien Barousse Avatar answered Sep 19 '22 07:09

Vivien Barousse


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();         }     } } 
like image 31
Mohammed Abbas Avatar answered Sep 22 '22 07:09

Mohammed Abbas