JsonMappingException没有单一的String构造函数/工厂方法Jackson [英] JsonMappingException no single-String constructor/factory method Jackson
问题描述
我正在尝试解析使用Spring build Jackson支持从我的Controller中的UI发送的JSON数据,这是我的代码
I am trying to parse JSON data being sent from UI in my Controller using Spring build Jackson support and this is my code
final Map<String, CartDataHelper> entriesToUpdateMap = new ObjectMapper().readValue(entriesToUpdate, new TypeReference<Map<String, CartDataHelper>>()
我的JSON字符串是
{"0":"{\"categoryCode\":\"shoes\",\"productCode\":\"300050253\",\"initialQty\":\"3\",\"leftoverQty\":\"0\",\"newQty\":\"3\"}",
"1":"{\"categoryCode\":\"shoes\",\"productCode\":\"300050254\",\"initialQty\":\"3\",\"leftoverQty\":\"0\",\"newQty\":\"3\"}"}
我使用一些在线服务检查了JSON格式并且它似乎有效,而tryin gto解析JSON数据我得到以下异常
i checked the JSON format using some online services and it seems valid, while tryin gto parse JSON data i am getting following exception
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class controllers.util.CartDataHelper] from JSON String; no single-String constructor/factory method
my CartDataHelper
class包含 productCode
, categoryCode
等的简单属性,带有无参数构造函数
my CartDataHelper
class contains simple properties for for productCode
, categoryCode
etc with a no argument constructor
推荐答案
正如评论所述,您的JSON包含 Map< String,String>
而不是 Map< String,CartDataHelper>
:值是JSON字符串,而不是JSON对象。
As comments mentioned, your JSON contains Map<String,String>
and NOT Map<String,CartDataHelper>
: values are JSON Strings, not JSON Objects.
理想情况下你不会尝试写作将对象作为JSON字符串;如果是这样,事情会奏效。
Ideally you would not try writing out objects as JSON Strings; and if so, things would work.
这篇关于JsonMappingException没有单一的String构造函数/工厂方法Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!