简体   繁体   English

在 Spring-boot 中使用整数键和字符串值反序列化 JSON

[英]Deserialization of JSON with integer keys and string value in Spring-boot

I have to de-serialize and serialize back a simple JSON having integer key and string value in Spring-boot using Feign library.我必须使用 Feign 库在 Spring-boot 中反序列化并序列化回具有整数键和字符串值的简单 JSON。 I know, if the key is in String, this is pretty straight forward in Spring-boot with automatic serialization and de-serialization.我知道,如果键在字符串中,这在 Spring-boot 中非常简单,具有自动序列化和反序列化功能。 My JSON looks like :我的 JSON 看起来像:

"avatar": {
            "48x48": "https://wfrjira.int.abc.com/secure/useravatar?ownerId=person&avatarId=1234",
            "24x24": "https://wfrjira.int.abc.com/secure/useravatar?size=small&ownerId=person&avatarId=1234"
        }

My POJO bean with proper getters and setters class is :我的具有适当 getter 和 setter 类的 POJO bean 是:

class Avatar {
private String _48x48;
private String _24x24;

public Avatar() {
}

public Avatar(String _48x48, String _24x24) {
    this._48x48 = _48x48;
    this._24x24 = _24x24;
}                               Getters and Setter ...

I am getting following error :我收到以下错误:

There was an unexpected error (type=Internal Server Error, status=500).出现意外错误(类型=内部服务器错误,状态=500)。 Error while extracting response for type [class [Lcom.XXXXXX.JiraResourceData;] and content type [application/json;charset=UTF-8];提取类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 的响应时出错; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法反序列化[Lcom.XXXXXX.JiraResourceData;实例[Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token;超出 START_OBJECT 令牌; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化[Lcom.XXXXXX.JiraResourceData;实例[Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token at [Source: (PushbackInputStream);在 [Source: (PushbackInputStream); 的 START_OBJECT 标记之外; line: 1, column: 1] feign.codec.DecodeException: Error while extracting response for type [class [Lcom.XXXXXX.JiraResourceData;] and content type [application/json;charset=UTF-8];行:1,列:1] feign.codec.DecodeException:提取类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 的响应时出错; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法反序列化[Lcom.XXXXXX.JiraResourceData;实例[Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token;超出 START_OBJECT 令牌; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化[Lcom.XXXXXX.JiraResourceData;实例[Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token at [Source: (PushbackInputStream);在 [Source: (PushbackInputStream); 的 START_OBJECT 标记之外; line: 1, column: 1]行:1,列:1]

I understand that I need to declare the variables in Avatar class with the name matching with the keys of JSON but strings can't be declared starting with numeric.我知道我需要在 Avatar 类中声明名称与 JSON 键匹配的变量,但不能声明以数字开头的字符串。 Is there any other way to resolve this in Spring-boot?有没有其他方法可以在 Spring-boot 中解决这个问题? Any help would be appreciated.任何帮助,将不胜感激。

First of all, your last JSON line has a comma.首先,你的最后一行JSON有一个逗号。 Remove that.去掉那个。 Furthermore, the JSON should be embedded in another set of brackets.此外, JSON应该嵌入到另一组括号中。

Also, annotate your class with @JsonRootName("avatar") since your class is uppercase but your JSON is lowercase.另外,用@JsonRootName("avatar")注释你的类,因为你的类是大写的,而你的JSON是小写的。

Furthermore, you probably want to rename your fields to the field names in the JSON or annotate these with @JsonProperty("my-json-name")此外,您可能希望将字段重命名为JSON的字段名称或使用@JsonProperty("my-json-name")注释这些字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM