1
 "results": 
[
        {
            "name": "xxx",
            "Data": {
                "id": 23
            }
        }
]

I have a json returning like this and I meet it with HashMap, but I can access the Data array. How can I get the id in the data array?

List<HashMap<String, Object>> personList = getCurrentResponse().jsonPath().getList("results");
   for (HashMap<String, Object> person : personList ) {
      if (person .get("name").equals(name)) {
          int id= (int) (person .get("Data.id"));
          return id;

I am getting NullException from the code sample above

2
  • 2
    You need to get the object keyed by Data, and then get the object of that keyed by id. Commented Aug 10, 2020 at 13:36
  • Can you help me with code? Commented Aug 10, 2020 at 13:38

2 Answers 2

1

You should first get Data as a HashMap, and then get id from it:

HashMap<String, Object> data = (HashMap<String, Object>) person .get("Data");
int id= (int) (data.get("id"));
Sign up to request clarification or add additional context in comments.

Comments

0

You can retrieve only one level at a time, you may call .get() twice

for (HashMap<String, Object> person : personList ) {
    if (person .get("name").equals(name)) {
        return person.get("Data").get("id");
    }
} 

1 Comment

@MehmetSerkanEkinci what do you mean ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.