简体   繁体   English

将 Object 转换为 JSON 文件,然后转换为字节以保存到存储,然后返回到 Object

[英]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:所以我想知道我是否可以这样做:

  1. Write value as String with jackson (Student to json)用 jackson 将值写入字符串(Student 到 json)
  2. Get the bytes of this String获取此字符串的字节
  3. Save to the Storage with save(studentName, bytes)使用 save(studentName, bytes) 保存到存储

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.

相关问题 当尝试从 S3 发送文件作为 email 附件时,字节类型的 Object 不是 JSON 可序列化的 - Object of type bytes is not JSON serializable when trying to send a file from S3 as an email attachment 如何将 DynamoDB JSON 转换为常规 Javascript object - How to convert DynamoDB JSON to a regular Javascript object 如何将下载的文档object转换成可用的JSON - How to convert downloaded document object into usable JSON GCP 存储 - 锁定存储在 GCP 存储桶中的对象/文件 - GCP storage - lock object/file stored in GCP bucket 将文件上传到 firebase 存储,存储异常对象在该位置不存在 - upload file to firebase storage, Storage exception Object does not exist at location Kafka Connect:将消息从字节转换为 Json - Kafka Connect: Convert message from bytes to Json 在 Python 中将 String 转换为 Object - Convert String to Object in Python 需要将此 model object 从 Json 格式转换为 Z35900D987289A83AF101D18A9F7C - need to convert this model object from Json format in Dart 如何使用 Glue 作业将 JSON 从 s3 转换为 CSV 文件并将其保存在同一个 s3 存储桶中 - How to convert JSON to CSV file from s3 and save it in same s3 bucket using Glue job 当谷歌云存储 object 重命名时停止 object 完成事件 - Stop object finalize event when google cloud storage object is renamed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM