Advertisement
Advertisement


how to convert object to string in java


Question

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
2015/03/18
1
33
3/18/2015 9:26:46 AM

Accepted 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.

2014/07/21
49
7/21/2014 4:40:18 PM

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();
        }
    }
}
2014/08/08

maybe you benefit from converting it to JSON string

String jsonString = new com.google.gson.Gson().toJson(myObject);

in my case, I wanted to add an object to the response headers but you cant add objects to the headers,

so to solve this I convert my object to JSON string and in the client side I will return that string to JSON again

2017/10/10

Looking at the output, it seems that your "temp" is a String array. You need to loop across the array to display each value.

2011/05/19

The result is not a String but a String[]. That's why you get this unsuspected printout.

[Ljava.lang.String is a signature of an array of String:

System.out.println(new String[]{});
2011/05/19

To convert serialize object to String and String to Object

stringToBean(beanToString(new LoginMdp()), LoginMdp.class);

public static String beanToString(Object object) throws IOException {

    ObjectMapper objectMapper = new ObjectMapper();
    StringWriter stringEmp = new StringWriter();
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    objectMapper.writeValue(stringEmp, object);
    return stringEmp.toString();
}

public static <T> T stringToBean(String content, Class<T> valueType) throws IOException {
    return new ObjectMapper().readValue(content, valueType);
}
2015/08/14

Source: https://stackoverflow.com/questions/6055476
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]