Spring Boot中JSON的反序列化,使用整数键和字符串值

huangapple go评论77阅读模式
英文:

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

问题

以下是翻译的内容:

我必须在Spring Boot中使用Feign库对具有整数键和字符串值的简单JSON进行反序列化和序列化。我知道,如果键是字符串,那么在Spring Boot中使用自动序列化和反序列化是非常直接的。我的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"
}

我的POJO Bean类具有适当的getter和setter方法:

class Avatar {
    private String _48x48;
    private String _24x24;

    public Avatar() {
    }

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

    // Getter和Setter方法...
}

我得到了以下错误:

> 发生了意外错误(类型=内部服务器错误,状态=500)。
> 在提取响应类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;];嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;],位置:[Source: (PushbackInputStream); 行:1,列:1]
feign.codec.DecodeException: 在提取响应类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;];嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;],位置:[Source: (PushbackInputStream); 行:1,列:1]

我理解我需要在Avatar类中使用与JSON键匹配的名称声明变量,但字符串不能以数字开头。在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. I know, if the key is in String, this is pretty straight forward in Spring-boot with automatic serialization and de-serialization. My JSON looks like :

"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 :

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).
Error while extracting response for type [class [Lcom.XXXXXX.JiraResourceData;] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token at [Source: (PushbackInputStream); 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]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData; out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 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. Is there any other way to resolve this in Spring-boot? Any help would be appreciated.

答案1

得分: 2

首先,您最后的JSON行有一个逗号。请移除该逗号。此外,应将该JSON嵌套在另一组括号内。

另外,为您的类添加注解@JsonRootName("avatar"),因为您的类名是大写的,但您的JSON是小写的。

此外,您可能希望将字段重命名为JSON中的字段名,或者使用注解@JsonProperty("my-json-name")进行标注。

英文:

First of all, your last JSON line has a comma. Remove that. Furthermore, the JSON should be embedded in another set of brackets.

Also, annotate your class with @JsonRootName("avatar") since your class is uppercase but your JSON is lowercase.

Furthermore, you probably want to rename your fields to the field names in the JSON or annotate these with @JsonProperty("my-json-name")

huangapple
  • 本文由 发表于 2020年8月26日 23:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63601040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定