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

无法反序列化START_OBJECT标记外的`java.lang.string`实例

伍皓
2023-03-14

我收到下面的错误信息,请有人帮助或建议如何最好地调试这个。

我正在尝试从REst API调用反序列化Products对象。在我添加代码反序列化Price子类之前,代码一直运行良好。如下所示,

"price": {
        "was": "",
        "then1": "",
        "then2": "",
        "now": "59.00",
        "uom": "",
        "currency": "GBP"
      },

我的Price pojo如下所示,

public class Price {

     @JsonProperty("was")
     String was;
     @JsonProperty("then1")
     String then1;
     @JsonProperty("then2")
     String then2;
     @JsonProperty("now")
     String now;
     @JsonProperty("uom")
     String uom;
     @JsonProperty("currency")
     String currency;

     public Price() {
         //blank constructor for JSON
     }

     @Override
     public String toString() {
         return "Price{" +
                 "was='" + was + '\'' +
                 ", then1='" + then1 + '\'' +
                 ", then2='" + then2 + '\'' +
                 ", now='" + now + '\'' +
                 ", uom='" + uom + '\'' +
                 ", currency='" + currency + '\'' +
                 '}';
     }
}

我编写了一个Junit测试来尝试并模拟该错误,

    @Test
    public void shouldConvertJsonProductListIntoPrice() {
        ObjectMapper objectMapper = new ObjectMapper();
        String content3 = "{\"products\": [{\"productId\": \"3525085\",\"title\": \"hush Tasha Vest Dress\", " +
                "\"price\": {\"was\": \"\",\"then1\": \"\",\"then2\": \"\",\"now\": \"59.00\",\"uom\": \"\",\"currency\": \"GBP\"}, " +
                "\"colorSwatches\": [{\"basicColor\": \"Red\",\"skuId\": \"237494589\"},{\"basicColor\": \"Blue\",\"skuId\": \"237494562\"}] " +
                "}]}";
        JavaType valueType = objectMapper.constructType(ProductList.class);
        ProductList readValue;
        try {
            readValue = objectMapper.readValue(content3, valueType);
            assertEquals("3525085", readValue.getProductList().get(0).productId);
            assertEquals("hush Tasha Vest Dress", readValue.getProductList().get(0).title);
            assertEquals("", readValue.getProductList().get(0).price.then1);
            assertEquals("59.00", readValue.getProductList().get(0).price.now);
            assertEquals("Blue", readValue.getProductList().get(0).colorSwatches[1].basicColor);
            assertEquals("237494562", readValue.getProductList().get(0).colorSwatches[1].skuId);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
public class Product {

     @JsonProperty("productId")
     String productId;

     @JsonProperty("title")
     String title;

     @JsonProperty("colorSwatches")
     ColorSwatch [] colorSwatches;

     @JsonProperty("price")
     Price price;

     public Product(){
         // blank required for Jackson
     }

     public Product(String productId, String title, ColorSwatch[] colorSwatches, Price price){
         this.productId = productId;
         this.title = title;
         this.colorSwatches = colorSwatches;
         this.price = price;
  }

共有1个答案

江嘉悦
2023-03-14

错误声明它需要一个值(最好是VALUE_STRING),而它得到一个START_OBJECT,因此您的问题可能来自以下形式的json

"price": {
    "was": "",
    "then1": "",
    "then2": "",
    "now":  {
        ...
    }
    "uom": "",
    "currency": "GBP"
},

而不是代码所需的“now”:“some value”形式。

 类似资料: