首页 > 解决方案 > 我应该如何将此 JSON 转换为 POJO 类?

问题描述

这是我的 JSON 回复

{
   "value":{
            "Status":1
             "Affected segments":[]
          "Message":[]
        }
     }

但这就是未来可能的样子

{
"value":{
        "Status":2
                "Affected segments":[{a},{b},{c}]
            "Message":[
                {
                "Content": "suhrfuahfuhasufhalushflashlfuhasurhaus",
                "Created date": "12/2/21"
                },{
                "Content": "suhrfuahfuhasufhalushflashlfuhasurhaus",
                "Created date": "15/2/21"
                },
               ]
        }
}

正如您在第一个代码片段中看到的那样,“状态”为 1 表示没有异常,而在第二个代码片段中,“状态”为 2 表示更改,因此数组已填充。

我将如何解析他们两个?

标签: javaserializationretrofit2

解决方案


Could you just create simply entity Message which would store the 'Content' and the 'Date' ? Then you could store the messages in List<Message> and check whether this is empty or not, to validate the condition you have.

class Message {
 String content;
 LocalDate date;
}

推荐阅读