0

I have method that is setting variable as an object

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

returned object is in the following format

[{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). How can I do that?

2
  • What's preventing you from calling toString() and parsing out the values? Commented Mar 16, 2022 at 16:57
  • HI @vb381, Can you please mark the answer as accepted if you satisfied my answer. Commented Mar 24, 2022 at 12:33

1 Answer 1

1

MyClass.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:

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).

Sign up to request clarification or add additional context in comments.

2 Comments

It's a great idea to construct an object from the values and put them into a list, but can you please edit your answer to include an explanation about why this solution works.
@sorifiend Added explaination

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.