【问题标题】:Convert string json inside a json to a object java将json中的字符串json转换为对象java
【发布时间】:2021-09-18 03:01:20
【问题描述】:

我有一个这样的字符串

"{
    "scanReference": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "document": {
        "status": "data",
        "data": {
            "firstName": "FULL NAME",
            "info": "{\"city\":\"CITY\",\"issueDate\":\"2019-01-01\"}"
        },
        "country": "USA"
    }
}"

我创建了类(简单 POJO)来将其转换为 java 对象并使用 GSON。我正在使用下面的代码转换为 java 对象。

(gson.fromJson(response, MyClass.class));

但我在转换时收到以下错误,这是因为“info”是一个字符串而不是 json。

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

当我将“info”创建为 json 并对其进行测试时,代码按预期工作,但这里是字符串。

谁能帮我解决这个问题?

PS:谢谢你的帮助。

【问题讨论】:

    标签: java spring spring-mvc jackson gson


    【解决方案1】:

    在 MyClass 中创建信息类。

    Public class MyClass.class{
    private String scanReference;
    ..........
    private Info info
    
    //Setter getter
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 2015-10-07
      • 2019-08-27
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      相关资源
      最近更新 更多