无法从 START_OBJECT 令牌中反序列化 `java.lang.String` 的实例
Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
我收到以下错误消息,有人可以帮忙或建议如何最好地调试它。
Cannot deserialize instance of java.lang.String
out of START_OBJECT
token at [Source: (PushbackInputStream); line: 1, column: 37610]
(through reference chain:
com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price["now"])
我正在尝试从 REst API 调用反序列化 Products 对象。在我添加代码以反序列化 Price sub-class 之前,代码一直运行良好。这看起来如下,
"price": {
"was": "",
"then1": "",
"then2": "",
"now": "59.00",
"uom": "",
"currency": "GBP"
},
我的价格表如下,
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();
}
}
如果我注释掉 "now" 字段,那么我的 RestAPI 调用将完美运行,我没有看到异常。 所以似乎有"now" 字段有问题,在这里我注意到它试图将“59.00”转换为字符串。这可能是 Fasterxml 转换器的问题吗?我可能需要帮忙吗?
产品 class 如下(尽管这是我从 API 电话中收到的字段列表大大减少了)。
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;
}
错误指出它期望 VALUE(最好是 VALUE_STRING),而它得到 START_OBJECT,所以您的问题可能来自 json 形式
"price": {
"was": "",
"then1": "",
"then2": "",
"now": {
...
}
"uom": "",
"currency": "GBP"
},
而不是代码预期的 "now": "some value"
形式。
我收到以下错误消息,有人可以帮忙或建议如何最好地调试它。
Cannot deserialize instance of
java.lang.String
out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 37610] (through reference chain: com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price["now"])
我正在尝试从 REst API 调用反序列化 Products 对象。在我添加代码以反序列化 Price sub-class 之前,代码一直运行良好。这看起来如下,
"price": {
"was": "",
"then1": "",
"then2": "",
"now": "59.00",
"uom": "",
"currency": "GBP"
},
我的价格表如下,
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();
}
}
如果我注释掉 "now" 字段,那么我的 RestAPI 调用将完美运行,我没有看到异常。 所以似乎有"now" 字段有问题,在这里我注意到它试图将“59.00”转换为字符串。这可能是 Fasterxml 转换器的问题吗?我可能需要帮忙吗?
产品 class 如下(尽管这是我从 API 电话中收到的字段列表大大减少了)。
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;
}
错误指出它期望 VALUE(最好是 VALUE_STRING),而它得到 START_OBJECT,所以您的问题可能来自 json 形式
"price": {
"was": "",
"then1": "",
"then2": "",
"now": {
...
}
"uom": "",
"currency": "GBP"
},
而不是代码预期的 "now": "some value"
形式。