gpt4 book ai didi

java - 我需要使用 GSON 将图像数组解析为 json

转载 作者:行者123 更新时间:2023-11-29 19:37:03 25 4
gpt4 key购买 nike

获取图片的方法

private List<HashMap<String, RequestBody>> getUserDocumentsPhoto(File[] images){

HashMap<String, RequestBody> userDocumentsPhoto = new HashMap<>();
List<HashMap<String, RequestBody>> photos = new ArrayList<>();

for (File image : images) {
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), image);
userDocumentsPhoto.put("filename=\"" + image.getName(), fileBody);
}
photos.add(userDocumentsPhoto);
return photos;

解析为json时需要获取

"document_photos": [
"/system/attachments/files/000/000/110/original/photo20160726_120701-1392731703.jpg?1469524104",
"/system/attachments/files/000/000/111/original/photo20160726_120814-2041790628.jpg?1469524105"]

但是当它解析时,“document_photos []”为空

最佳答案

您必须创建模型类。这个类的结构是这样的:

public class Model {
ArrayList<String> items = new ArrayList<>();

public ArrayList<String> getItems() {
return items;
}

public void setItems(ArrayList<String> items) {
this.items = items;
}
}

你的 Activity 是这样的:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Model model = new Model();
model.getItems().add("first");
model.getItems().add("second");
Gson gson = new Gson();
String json = gson.toJson(model);
Log.e("json",json);
}
}

在你的 build.gradle 中添加 Gson 库作为依赖。

关于java - 我需要使用 GSON 将图像数组解析为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39076394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com