[英]Convert Object to JSON File then to Bytes to save to an Storage and then back to Object
Suppose I have the class Student which only has name and age attributes.假设我有 class Student,它只有 name 和 age 属性。
public class Student {
private String studentName;
private Integer age;
}
I want to convert this Student objects to JSON files (I think I can do this with Jackson mapper)我想将这个 Student 对象转换为 JSON 文件(我想我可以用 Jackson 映射器做到这一点)
Then I need to store this.json file to an storage, so I have the method save(String key, byte[] resource) which saves any kind of file to the storage (.zip, .txt, etc.).然后我需要将 this.json 文件存储到存储中,所以我有方法 save(String key, byte[] resource) 将任何类型的文件保存到存储中(.zip、.txt 等)。
So I was wondering if I can do this like so:所以我想知道我是否可以这样做:
Back to Student object:回到学生object:
Then when I search an student from the storage I would need to convert back to String like so:然后,当我从存储中搜索学生时,我需要像这样转换回字符串:
Charset charset = Charset.forName("UTF-8");
String string = new String(studentBytes, charset);
Then String back to Student with Jackson mapper然后使用 Jackson 映射器将字符串返回给 Student
Is this a good approach (I think not because the idea is that those bytes are actually a.json file, but are bytes of a java String object)?这是一个好方法吗(我认为不是因为这些字节实际上是一个 .json 文件,而是一个 java String 对象的字节)? I am a little confused because the "file bytes" that I am saving are from a String object with this approach, but I can not find how to convert the Student object to a ".json file", can not figure out how to save.json file (which I do not need to save in disk because I only need the bytes to save to the cloud storage), how would I write an Object to an "in memory.json file" or maybe is not to see it as a file, just as bytes right?我有点困惑,因为我保存的“文件字节”来自字符串 object,但我找不到如何将学生 object 转换为“.json 文件”,不知道如何保存.json 文件(我不需要将其保存在磁盘中,因为我只需要将字节保存到云存储中),我如何将 Object 写入“在 memory.json 文件中”,或者可能不将其视为文件,就像字节一样对吗? Please point in the right direction, like how to directly write the object to a json file and then store this file bytes?请指出正确的方向,比如如何将object直接写入一个json文件,然后存储这个文件字节?
Yes this is how you want to do it if you want to save json to a file to upload to S3.是的,如果您想将 json 保存到文件以上传到 S3,这就是您想要的方式。 Jackson has a native json data structure (I think it's called JsonNode
) but you don't want to save that to a file. Jackson 具有本机 json 数据结构(我认为它称为JsonNode
)但您不想将其保存到文件中。 Just save the json string to a text file.只需将 json 字符串保存到文本文件中即可。
To convert Student
object to String:将Student
object 转换为字符串:
Student student = new Student("Joe", 25);
ObjectMapper mapper = new ObjectMapper();
String json_string = mapper.writeValueAsString(student);
You can save this string to a text file and upload to S3.您可以将此字符串保存到文本文件并上传到 S3。 When you want to get the Student
object back:当您想取回Student
object 时:
ObjectMapper mapper = new ObjectMapper();
Student student = mapper.readValue(new File("student.json"), Student.class);
Note that there is nothing special about a "json file".请注意,“json 文件”没有什么特别之处。 It's just a text file with the .json
extention.它只是一个扩展.json
的文本文件。 It contains text data that happens to be in the Json format.它包含恰好采用 Json 格式的文本数据。 You can save json_string
to a file like this:您可以将json_string
保存到这样的文件中:
Path path = Paths.get("student.json");
byte[] strToBytes = json_string.getBytes();
Files.write(path, strToBytes);
(Reference: https://www.baeldung.com/jackson-object-mapper-tutorial ) (参考: https://www.baeldung.com/jackson-object-mapper-tutorial )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.