简体   繁体   中英

Java/jackson object mapping: how to convert a nested JSON array to a list of objects (might be best explain with an example)

2 questions:

  • how can I have a list of objects inside another list of objects? (in my example JSON, this would be list of Animal objects with a list of Specie objects inside it)
  • how can I remove the static declaration in the inner class and have stuff still working ok?

using the code below, I can have a list of Animal objects with a Specie array inside it.


sample JSON:

[
    {"name":"Arthropod"
        ,"species":[
            {"name":"spider","class":"Arachnida"}
            ,{"name":"butterfly","class":"Insecta"}
            ]
    }
    ,{"name":"Mammal"
        ,"species":[
            {"name":"monkey","class":"Primates"}
            , {"name":"kangaroo","class":"Diprotodontia"}
        ]
    }
]

my code:

public class Animal
    private String name
    private Specie[] species;
    
    @JsonCreator
    public Animal(
        @JsonProperty("name") String name
        , @JsonProperty("specifies") Specie[] pets) {
        this.name = name;
        this.pets = pets;
    };

    static class Specie {
        private String name;
        private String class;
    }
    

List<Animal> animals = new ObjectMapper().readValue(myJSON, new TypeReference<List<Animal>>(){});

How about this:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

public class Animal {
  private String name;
  private List<Specie> species;

  @JsonCreator
  public Animal(@JsonProperty("name") String name,
      @JsonProperty("species") List<Specie> pets) {
    this.name = name;
    this.species = pets;
  }

  @Override
  public String toString() {
    return "\n//Animal\n"
        + "{" +
        "name='" + name + '\'' +
        ", species=" + species +
        '}';
  }
}

class Specie {
  @JsonProperty("name")
  private String name;

  @JsonProperty("class")
  private String _class;

  @Override
  public String toString() {
    return "\n//Specie\n{" +
        "name='" + name + '\'' +
        ", _class='" + _class + '\'' +
        '}';
  }
}

class Main {
  public static void main(String[] args) throws IOException {
    String myJSON = "[\n"
        + "    {\"name\":\"Arthropod\"\n"
        + "        ,\"species\":[\n"
        + "            {\"name\":\"spider\",\"class\":\"Arachnida\"}\n"
        + "            ,{\"name\":\"butterfly\",\"class\":\"Insecta\"}\n"
        + "            ]\n"
        + "    }\n"
        + "    ,{\"name\":\"Mammal\"\n"
        + "        ,\"species\":[\n"
        + "            {\"name\":\"monkey\",\"class\":\"Primates\"}\n"
        + "            , {\"name\":\"kangaroo\",\"class\":\"Diprotodontia\"}\n"
        + "        ]\n"
        + "    }\n"
        + "]";
    List<Animal> animals = new ObjectMapper().readValue(myJSON, new TypeReference<List<Animal>>(){});
    System.out.println(animals);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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