当前位置: 首页 > 知识库问答 >
问题:

UnrecognizedPropertyException来自Jackson

季俭
2023-03-14
{
  "status": "ok",
  "result": {
    "result": [
      {
        "product_id": "1420-131617-82",
        "sku": "1420-131617",
        "display_sku": "8DD 355 100-411",
        "genart_number": 82,
        "name": "Bremsscheibe",
        "description": null,
        "additional_info_text": null,
        "product_url": "https://www.autoteile5000.de/product/1420-131617-82",
        "image_url": "https://static.autoteile5000.de/product-images/HLP/4625-462502682-3-255-1548045462267.jpg",
        "brand": "HELLA PAGID",
        "eans": [
          "4082300365078"
        ],
        "manufacturer_product_number": "8DD 355 100-411",
        "data_supplier_number": "4625",
        "pricehammer": false,
        "buyable": true,
        "bulky_good": false,
        "risky_good": false,
        "hazardous_good": false,
        "car_specific": true,
        "has_deposit": false,
        "is_exchange_part": false,
        "visibility_status": "active",
        "deleted": false
      }
    ]
  }
}
public List<SimpleProductDto> getProducts(ProductForm productForm) {

    JsonParser jsonParser = new JsonParser();
    try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("data/product/2210-0929-818/product.json") ) {
        String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
        //System.out.println("print text : " + text);
        //Read JSON file
        JsonObject objectFromString = jsonParser.parse(text).getAsJsonObject();
       // System.out.println(objectFromString.toString());

        JsonObject objectFromString1 = objectFromString.getAsJsonObject("result");
       // System.out.println(objectFromString.toString());

       // System.out.println(objectFromString1.toString());

        JsonArray jsonArray = objectFromString1.getAsJsonArray("result");
        System.out.println("printing json array : " +jsonArray.toString());

        ObjectMapper oMapper = new ObjectMapper();
        for(JsonElement element : jsonArray){
            JsonObject productObj = element.getAsJsonObject();
            System.out.println("printing json object : " + productObj.toString());
            SimpleproductDtoMapper productDtoList = oMapper.readValue(productObj.toString(), SimpleproductDtoMapper.class);
        }
      // List<SimpleproductDtoMapper> productDtoList = oMapper.readValue(jsonArray.toString(), new TypeReference<List<SimpleproductDtoMapper>>() {});

       // Map<String, SimpleproductDtoMapper> items = productDtoList.stream().collect(Collectors.toMap(SimpleproductDtoMapper::getProductId, Function.identity()));

        //items.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));

        //Iterate over employee array
        //productList.forEach(emp -> parseProductObject((JSONObject) emp));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
public class SimpleproductDtoMapper {

@SerializedName("product_id")
private String productId;
private String sku;
@SerializedName("display_sku")
private String displaySku;
@SerializedName("genart_number")
private Integer genartNumber;
private String name;
private String description;
@SerializedName("additional_info_text")
private String additionalInfoText;
@SerializedName("product_url")
private String productUrl;
@SerializedName("image_url")
private String imageUrl;
private String brand;
private List<String> eans;
@SerializedName("manufacturer_product_number")
private String manufacturerProductNumber;
@SerializedName("data_supplier_number")
private String dataSupplierNumber;
private boolean pricehammer;
private boolean buyable;
@SerializedName("bulky_good")
private boolean bulkyGood;
@SerializedName("risky_good")
private boolean riskyGood;
@SerializedName("hazardous_good")
private boolean hazardousGood;
@SerializedName("car_specific")
private boolean carSpecific;
@SerializedName("has_deposit")
private boolean hasDeposit;
@SerializedName("is_exchange_part")
private boolean isExchangePart;
@SerializedName("visibility_status")
private VisibilityStatusDto visibilityStatus;
@SerializedName("deleted")
private boolean deleted;
}

我得到了一个例外:

在[source:(String)“{”product_id“:”1420-131617-82“,”sku“:”1420-131617“,”display_sku“:”8dd 355 100-411“,”genart_number“:82,”name“:”bremscheibe“,”description“:null,”additional_info_text“:null,”product_url“:”https://www.autoteile5000.de/product/1420-131617-82“,”image_url“:”M.FasterXml.Jackson.Databind.Deser.BeandeSerializerBase.HandleUnknownProperty(BeandeSerializerBase.1589)在com.FasterXml.Jackson.Databind.Deser.BeandeSerializerBase.HandleUnknownVanilla(BeandeSerializerBase.1567)在com.FasterXml.Jackson.Databind.Deser.BeandeSerializer.VanillaDerialize(BeandeSerializer.294)在

共有1个答案

耿联
2023-03-14

使用jsonschema2pojo,您可以生成带有jackson注释的POJO类。选择jackson2.x生成带有jackson注释的模型。源类型应为JSON。在下面,您可以看到我使用此工具生成的product类。所有属性都是public,因此将它们更改为private,并生成getterssetters

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}]
 类似资料:
  • 问题内容: 使用的我可以将任意的数据到数据的URL。有没有办法使用内置的浏览器API 将数据URL转换回实例? 问题答案: 用户Matt一年前提出了以下代码如何在javascript中将dataURL转换为文件对象?,这可能对您有帮助 编辑:正如一些评论者所报道的,BlobBuilder已被弃用了一段时间。这是更新的代码:

  • 问题内容: 这是我认为的Java纯粹主义者之一。最近,我遇到了一种将字符串值自定义解析为布尔值的方法的问题。一个足够简单的任务,但是由于某种原因,以下方法在空情况下抛出NullPointerException…… 该方法的返回类型为布尔值,那么为什么或如何引发NullPointerException?从调试开始,似乎在嵌套的嵌入式条件语句评估为null并向外部嵌入式条件语句返回null的时候抛出了

  • 问题内容: 由于某种原因,登录完成后无法重定向到/ blog。在我的登录控制器中,我有以下内容。 jQuery Ajax 更新为工作代码 问题答案: AJAX之后,您将无法进行重定向。您需要自己使用Javascript完成。 服务器 客户 这应该工作。

  • 删除了仅使用的构造函数。在中,只需编写 但是,该构造函数在4.0中不再有效。文档位于https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html 显示了其他几个构造函数,但理想情况下我希望更改尽可能少的行。 所以,我的问题是,现在(在ApachePOI4.0中)从创建的最佳方法是什么? 根据注释中的要

  • 使用的我可以将任意数据转换为Data URL。有没有办法使用内置浏览器apis将Data URL转换回实例?

  • 以前,我在Red Hat提供的Oracle Java SE中使用JavaFX开发应用程序,但似乎不再提供JavaFX了,如https://access.redhat.com/articles/3253281。然而,似乎来自rhel-7-server-rpms存储库的OpenJDK并不是JavaFX附带的。 是否有更好的方法来代替仅仅从Red Hat提供的存储库之外安装包?我不想用自编译的OpenJ