简体   繁体   English

JAVA - 如何将 object 转换为列表

[英]JAVA - How to convert object to list

I have method that is setting variable as an object我有将变量设置为 object 的方法

Object value = response.getBody().jsonPath.get(variable)

returned object is in the following format返回的 object 格式如下

[{text1=value1, text2=value2, text3=value3}, {text1=value4, text2=value5, text3=value6}]

I need somehow to itter over this end verify all values (value1-6).为此,我需要以某种方式验证所有值(值 1-6)。 How can I do that?我怎样才能做到这一点?

MyClass.java我的班级.java

public class MyClass {
    private String text1;
    private String text2;

    public MyClass() {
    }

    public MyClass(String text1, String text2) {
        this.text1 = text1;
        this.text2 = text2;
    }

    public String getText1() {
        return this.text1;
    }

    public void setText1(String text1) {
        this.text1 = text1;
    }

    public String getText2() {
        return this.text2;
    }

    public void setText1(String text2) {
        this.text2 = text2;
    }
}
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();

String jsonInput = response.getBody();
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

Explanation of How ObjectMapper works: ObjectMapper 工作原理说明:

The Jackson ObjectMapper class is the simplest way to parse JSON with Jackson. The Jackson Object mapper can parse JSON into objects of classes developed by you, or into objects of the built-in JSON tree model. The reason it is called ObjectMapper is that it maps JSON into Java Objects (deserialization), or Java Objects into JSON (serialization). The Jackson ObjectMapper class is the simplest way to parse JSON with Jackson. The Jackson Object mapper can parse JSON into objects of classes developed by you, or into objects of the built-in JSON tree model. The reason it is called ObjectMapper is that it将 JSON 映射到 Java 对象(反序列化),或将 Java 对象映射到 JSON(序列化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM