java 如何在Android中将自定义对象转换为String?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16543241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:15:51  来源:igfitidea点击:

How to convert a custom object into String in Android?

javaandroidobjectserialization

提问by Mukul Bansal

I have an Object of a custom class. I need to convert it into a String so that I can write it to a file. Is there a way out for this ?

我有一个自定义类的对象。我需要将其转换为字符串,以便将其写入文件。有没有办法解决这个问题?

thanks for help.

感谢帮助。

回答by zbr

You can't convert a custom class into a Stringautomatically. The system has no way of knowing how you want that Stringto be formatted, what exactly you want to be in it and such.

您不能将自定义类String自动转换为。系统无法知道您希望如何String对其进行格式化,您希望在其中包含什么内容等等。

You have to manually implement a method in your custom class that returns the text interpretation of the object. Usually you would override a method called toString(). This method often gets called automatically if you provide an instance of your class to some methods. Let's say if you do System.out.println(instance), then the instance's toString()method is automatically invoked.

您必须在自定义类中手动实现一个方法,该方法返回对象的文本解释。通常你会覆盖一个名为toString(). 如果您将类的实例提供给某些方法,则通常会自动调用此方法。假设你这样做了System.out.println(instance),那么instancetoString()方法会被自动调用。

I don't know what your custom class is. But let's say it's something like a Personclass with member variables String nameand int age. Then the toString()method defined in the class could look something like this:

我不知道你的自定义类是什么。但是假设它类似于一个Person带有成员变量String nameint age. 那么toString()类中定义的方法可能如下所示:

@Override
public String toString() {
    return "Name: " + name + ", age: " + age;
}

This would provide an output like "Name: Some Name, age: 30".

这将提供类似“姓名:某个姓名,年龄:30”的输出。

回答by Anthone

To convert serialize object to String and String to Object

将序列化对象转换为字符串并将字符串转换为对象

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);
}

And to save an object to a file

并将对象保存到文件

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
fos.close();
Loading (w/o exception handling code):

FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
SimpleClass simpleClass = (SimpleClass) is.readObject();
is.close();
fis.close();

回答by bakriOnFire

Try , using the objectname.toString()method.

尝试,使用objectname.toString()方法。