-1

I have a JSON object structured like this:

{
    "John": { ...Some JSON object... },
    "Mary": { ...Some JSON object... },
    "Sam": { ...Some JSON object... }
}

The values for the John, Mary, and Sam keys are all structured the same, let's say it maps to a PersonInfo Java class. How do I get a List<PersonInfo> by just getting the top level JSON objects keys as a List? I can't do something like this

import com.fasterxml.jackson.annotation.JsonProperty;

@Data
public class PersonRecord {
    @JsonProperty("John")
    private PersonInfo john;
}

Because I don't know the top level keys, so I can't hardcode John. I don't care about the name of the key, I just want to select the values from each of the top level keys and put them in a list.

5
  • import com.fasterxml.jackson.annotation.JsonProperty; @Data public class PersonRecord { @JsonProperty("John") private List<PersonInfo> listofperson; } Commented Jul 23, 2024 at 5:20
  • I can't hardcode John since I don't know the top level keys Commented Jul 23, 2024 at 5:25
  • import com.fasterxml.jackson.annotation.JsonProperty; @Data public class PersonRecord { @JsonProperty("John") private Map<String,PersonInfo> rec; } try this Commented Jul 23, 2024 at 5:30
  • Isn't that going to ignore the Mary, Sam etc keys? Commented Jul 23, 2024 at 5:32
  • no it will dynamically get mapped try once let me know Commented Jul 23, 2024 at 5:33

1 Answer 1

-1

Use Map<String, PersonInfo> instead of PersonRecord.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.