Используя jsonschema2pojo , вы можете создать POJO
класс с Jackson
аннотациями.Выберите Jackson 2.x
, чтобы создать модель с аннотациями Jackson
.Source type
должно быть JSON
.Ниже вы можете увидеть класс Product
, созданный мной с помощью этого инструмента.все свойства public
, поэтому измените их на private
и сгенерируйте getters
и setters
.
class Product {
@JsonProperty("product_id")
public String productId;
@JsonProperty("sku")
public String sku;
@JsonProperty("display_sku")
public String displaySku;
@JsonProperty("genart_number")
public Integer genartNumber;
@JsonProperty("name")
public String name;
@JsonProperty("description")
public Object description;
@JsonProperty("additional_info_text")
public Object additionalInfoText;
@JsonProperty("product_url")
public String productUrl;
@JsonProperty("image_url")
public String imageUrl;
@JsonProperty("brand")
public String brand;
@JsonProperty("eans")
public List<String> eans = null;
@JsonProperty("manufacturer_product_number")
public String manufacturerProductNumber;
@JsonProperty("data_supplier_number")
public String dataSupplierNumber;
@JsonProperty("pricehammer")
public Boolean pricehammer;
@JsonProperty("buyable")
public Boolean buyable;
@JsonProperty("bulky_good")
public Boolean bulkyGood;
@JsonProperty("risky_good")
public Boolean riskyGood;
@JsonProperty("hazardous_good")
public Boolean hazardousGood;
@JsonProperty("car_specific")
public Boolean carSpecific;
@JsonProperty("has_deposit")
public Boolean hasDeposit;
@JsonProperty("is_exchange_part")
public Boolean isExchangePart;
@JsonProperty("visibility_status")
public String visibilityStatus;
@JsonProperty("deleted")
public Boolean deleted;
@Override
public String toString() {
return "Product{" +
"productId='" + productId + '\'' +
", sku='" + sku + '\'' +
", displaySku='" + displaySku + '\'' +
", genartNumber=" + genartNumber +
", name='" + name + '\'' +
", description=" + description +
", additionalInfoText=" + additionalInfoText +
", productUrl='" + productUrl + '\'' +
", imageUrl='" + imageUrl + '\'' +
", brand='" + brand + '\'' +
", eans=" + eans +
", manufacturerProductNumber='" + manufacturerProductNumber + '\'' +
", dataSupplierNumber='" + dataSupplierNumber + '\'' +
", pricehammer=" + pricehammer +
", buyable=" + buyable +
", bulkyGood=" + bulkyGood +
", riskyGood=" + riskyGood +
", hazardousGood=" + hazardousGood +
", carSpecific=" + carSpecific +
", hasDeposit=" + hasDeposit +
", isExchangePart=" + isExchangePart +
", visibilityStatus='" + visibilityStatus + '\'' +
", deleted=" + deleted +
'}';
}
}
Я заметил, что вы хотите пропустить уровни result(Object) -> result(Array)
.Посмотрите, как это сделать с Jackson
:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
JsonNode jsonNode = mapper
.reader()
.at("/result/result")
.readTree(new FileInputStream(jsonFile));
CollectionType productsType = mapper.getTypeFactory().constructCollectionType(List.class, Product.class);
List<Product> products = mapper.convertValue(jsonNode, productsType);
System.out.println(products);
}
}
Над отпечатками кода:
[Product{productId='1420-131617-82', sku='1420-131617', displaySku='8DD 355 100-411', genartNumber=82, name='Bremsscheibe', description=null, additionalInfoText=null, productUrl='https://www.autoteile5000.de/product/1420-131617-82', imageUrl='https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg', brand='HELLA PAGID', eans=[4082300365078], manufacturerProductNumber='8DD 355 100-411', dataSupplierNumber='4625', pricehammer=false, buyable=true, bulkyGood=false, riskyGood=false, hazardousGood=false, carSpecific=true, hasDeposit=false, isExchangePart=false, visibilityStatus='active', deleted=false}]