如何为异构JSON对象创建POJO类?解决jsonschema2pojo无响应问题
Hey there, let's work through this problem! Your JSON array contains two distinct object structures, which is probably why jsonschema2pojo wasn't returning results—it needs explicit configuration to handle polymorphic (multiple-type) JSON. Below are two solutions: manual POJO creation (straightforward and controllable) and fixing the tool to generate classes for you.
Solution 1: Manual POJO Creation
Since your JSON has two related but different objects, we'll use a base class for shared fields, then create subclasses for each unique structure. We'll also add Jackson annotations to handle automatic deserialization based on the status field.
Step 1: Base Class (Shared Fields)
Create a Place class to hold fields common to both JSON objects:
import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "status" // Field used to distinguish object types ) @JsonSubTypes({ @JsonSubTypes.Type(value = Park.class, name = "1"), @JsonSubTypes.Type(value = ParkFacility.class, name = "2") }) public class Place { private String title; private int status; // Getters and Setters public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
Step 2: Subclass for Status=1 Objects
Create a Park class for the first JSON structure:
public class Park extends Place { private String alamat; private String image; // Getters and Setters public String getAlamat() { return alamat; } public void setAlamat(String alamat) { this.alamat = alamat; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }
Step 3: Subclass for Status=2 Objects
First, create a Fasilitas class for the nested array items:
public class Fasilitas { private String namaFasilitas; private String imageFasilitas; // Getters and Setters public String getNamaFasilitas() { return namaFasilitas; } public void setNamaFasilitas(String namaFasilitas) { this.namaFasilitas = namaFasilitas; } public String getImageFasilitas() { return imageFasilitas; } public void setImageFasilitas(String imageFasilitas) { this.imageFasilitas = imageFasilitas; } }
Then create the ParkFacility subclass:
import java.util.List; public class ParkFacility extends Place { private List<Fasilitas> fasilitas; // Getters and Setters public List<Fasilitas> getFasilitas() { return fasilitas; } public void setFasilitas(List<Fasilitas> fasilitas) { this.fasilitas = fasilitas; } }
With these classes, you can use Jackson to deserialize your JSON array into a List<Place>—the library will automatically map each item to the correct subclass based on the status value.
Solution 2: Fix jsonschema2pojo to Generate POJOs
The tool fails with your raw JSON because it can't infer polymorphic types automatically. To fix this:
Step 1: Convert Your JSON to a JSON Schema
Create a schema that explicitly defines the two object types and uses the status field as a discriminator:
{ "type": "array", "items": { "type": "object", "discriminator": { "propertyName": "status" }, "oneOf": [ { "$ref": "#/definitions/Park" }, { "$ref": "#/definitions/ParkFacility" } ], "definitions": { "Park": { "type": "object", "properties": { "title": { "type": "string" }, "alamat": { "type": "string" }, "image": { "type": "string" }, "status": { "type": "integer", "enum": [1] } }, "required": ["title", "status"] }, "ParkFacility": { "type": "object", "properties": { "title": { "type": "string" }, "fasilitas": { "type": "array", "items": { "type": "object", "properties": { "namaFasilitas": { "type": "string" }, "imageFasilitas": { "type": "string" } } } }, "status": { "type": "integer", "enum": [2] } }, "required": ["title", "status"] } } } }
Step 2: Configure jsonschema2pojo
In the tool's interface:
- Set Source type to
JSON Schema - Check Enable Jackson annotations
- Enter your target package name
- Paste the schema above and generate
The tool will now produce POJOs identical to the manual ones we created, including the polymorphic Jackson annotations.
内容的提问来源于stack exchange,提问作者Danny Aditya

