java map 序列化json,java – 使用Jackson反序列化JSON Map并没有给出正确的Map

问题

>首先,下面的序列化JSON序列化是否有意义?

>如果是这样,为什么我没有收回地图?

>在反序列化方面我能做些什么?

Map< String,String>的JSON序列化财产(摘录):

{

"attributes": {

"entry": [

{

"key": "operating system",

"value": "GNU/Linux"

},

{

"key": "allergies",

"value": "weed"

}

]

}

}

用于反序列化的POJO:

class Contact implements Comparable, Serializable {

@JsonProperty("attributes")

private Map attributes;

...

}

导致此异常:

Thread-4889 An exception occurred during request network execution :Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

at [Source: libcore.net.http.FixedLengthInputStream@43822760; line: 1, column: 17] (through reference chain: com.example.model.Contact["attributes"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

at [Source: libcore.net.http.FixedLengthInputStream@43822760; line: 1, column: 17] (through reference chain: com.example.model.Contact["attributes"])

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token

at [Source: libcore.net.http.FixedLengthInputStream@43822760; line: 1, column: 17] (through reference chain: com.example.model.Contact["attributes"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

at [Source: libcore.net.http.FixedLengthInputStream@43822760; line: 1, column: 17] (through reference chain: com.example.model.Contact["attributes"])

at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126)

at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)

at com.example.providers.Query$1.loadDataFromNetwork(Query.java:99)

at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)

at com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)

at com.octo.android.robospice.request.RequestRunner$1.run(RequestRunner.java:197)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)

at java.util.concurrent.FutureTask.run(FutureTask.java:234)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

at java.lang.Thread.run(Thread.java:841)

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token

at [Source: libcore.net.http.FixedLengthInputStream@43822760; line: 1, column: 17] (through reference chain: com.example.model.Contact["attributes"])

at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)

at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)

at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)

at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:430)

at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:312)

at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)

at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)

at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:106)

at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)

at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)

at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)

at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.des

反序列化后在调试器中检查时的属性Object:

进入后进一步检查:

@JsonProperty("attributes")

private Map>> attributes;

依赖关系:

> com.fasterxml.jackson.core:jackson-core:2.3.0

> com.fasterxml.jackson.core:jackson-databind:2.3.0

> com.fasterxml.jackson.core:jackson-annotations:2.3.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的Map是一种非常方便的数据结构,它可以存储键值对,这些键值对可以根据需要添加、删除或修改。而JSON是一种轻量级的数据交换格式,它被广泛用于跨平台和跨语言的数据传输。Map转换为JSON是开发过程中常用的操作之一,下面简单介绍一下如何实现Map转换为JSON。 在Java中,我们可以使用第三方库Gson或者Jackson来实现Map转换为JSON。其中,Gson是Google提供的开源类库,它提供了fromJson、toJson、toJsonTree等方法,可以将Java对象转换为JSON格式的字符串或解析JSON格式的字符串并生成Java对象。Jackson是另一个非常优秀的Java序列化框架,它也提供了将Java对象转换为JSON格式的方法,并提供了很多高级特性,如对JSON数据中的日期格式、NULL值、枚举类型等的处理。 下面给出一个使用Gson进行Map转换为JSON的例子: ``` Map<String, Object> map = new HashMap<>(); map.put("name", "Tom"); map.put("age", 20); String jsonStr = new Gson().toJson(map); ``` 在上面的代码中,我们首先创建了一个Map对象,并向其中添加了一些键值对。然后使用Gson的toJson方法将Map对象转换为JSON格式的字符串。这个jsonStr字符串的结果是: ``` {"age":20,"name":"Tom"} ``` 如果使用Jackson进行Map转换为JSON,可以使用如下代码: ``` Map<String, Object> map = new HashMap<>(); map.put("name", "Tom"); map.put("age", 20); String jsonStr = new ObjectMapper().writeValueAsString(map); ``` 在上面的代码中,我们使用Jackson中的ObjectMapper类,并调用其writeValueAsString方法将Map对象转换为JSON格式的字符串。这个jsonStr字符串的结果与使用Gson转换的结果相同。 综上所述,无论你选择使用Gson还是JacksonMap转换为JSON都非常方便。通过简短的代码就可以将Java程序中的Map对象转换为可用于网络传输的JSON格式数据。在实际开发中,我们可以选择适合自己的方法和工具来完成Map转换为JSON的操作,提高我们的开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值