将JSON映射到POJO以进行处理 – 未标记为可忽略的未知字段

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

Map JSON to POJO For Processing - Unrecognized field not marked as ignorable

问题

我需要帮助将下面的 JSON 结构映射到后续处理中

[{
    "userId": "11",
    "otherId": "a",
    "key1": "特斯拉",
    "key2": "S3",
    "behaviour": {
        "color": "白色",
        "size": "S",
        "owner": "A 先生"
    }
}, 
{
    "userId": "22",
    "otherId": "",
    "key1": "梅赛德斯",
    "key2": "C-Class",
    "behaviour": {
        "color": "黑色",
        "size": "M",
        "isNew": true
    }
},
{
    "userId": "33",
    "otherId": "c",
    "key1": "本田",
    "key2": "CRV",
    "behaviour": {
        "color": "绿色",
        "size": "L"
    }
}]

以下是我拥有的 POJO

public class MainObject {
    private String userId;
    private String otherId;
    private String key1;
    private String key2;
    private Set<Behaviour> behaviours;
}

public class Behaviour {
    private final String name;
    private final Object value;
}

我需要获取 MainObject 列表以进行进一步处理尝试了以下代码但不确定如何映射 behavior 集合 -

String inputLine = currentBufferedReader.readLine();
// 以上字符串包含完整的 JSON

ObjectMapper objectMapper = new ObjectMapper();
MainObject[] mainObjects = objectMapper.readValue(inputLine, MainObject[].class);

我得到Unrecognized field not marked as ignorable with above code. 请提供建议
英文:

I need help in mapping below JSON structure for further processing.

    [{
&quot;userId&quot;: &quot;11&quot;,
&quot;otherId&quot;: &quot;a&quot;,
&quot;key1&quot;: &quot;Tesla&quot;,
&quot;key2&quot;: &quot;S3&quot;,
&quot;behaviour&quot;: {
&quot;color&quot;: &quot;white&quot;,
&quot;size&quot;: &quot;S&quot;,
&quot;owner&quot;: &quot;Mr. A&quot;
}
}, 
{
&quot;userId&quot;: &quot;22&quot;,
&quot;otherId&quot;: &quot;&quot;,
&quot;key1&quot;: &quot;Merc&quot;,
&quot;key2&quot;: &quot;C-Class&quot;,
&quot;behaviour&quot;: {
&quot;color&quot;: &quot;black&quot;,
&quot;size&quot;: &quot;M&quot;,
&quot;isNew&quot;: true
}
},
{
&quot;userId&quot;: &quot;33&quot;,
&quot;otherId&quot;: &quot;c&quot;,
&quot;key1&quot;: &quot;Honda&quot;,
&quot;key2&quot;: &quot;CRV&quot;,
&quot;behaviour&quot;: {
&quot;color&quot;: &quot;green&quot;,
&quot;size&quot;: &quot;L&quot;,
}
}]

Below is the POJO i have:

public class MainObject {
private String userId;
private String otherId;
private String key1;
private String key2;
private Set&lt;Behaviour&gt; behaviours;
}
public class Behaviour {
private final String name;
private final Object value;
}

I need to get the list of MainObject for further processing. Tried below but not sure how can i map for behaviour set -

String inputLine = currentBufferedReader.readLine();
//Above String has complete JSON
ObjectMapper objectMapper = new ObjectMapper();
FirstObject[] firstObjects = objectMapper.readValue(inputLine, FirstObject[].class);

I am getting: Unrecognized field not marked as ignorable with above code. Please suggest.

答案1

得分: 1

定义您的类如下所示

    class MainObject {
        public String userId;
        public String otherId;
        public String key1;
        public String key2;
        @JsonProperty(value = "behaviour")
        public Map<String, String> behaviours;
        @Override
        public String toString() {
            return "MainObject [userId=" + userId + ", otherId=" + otherId + ", key1=" + key1 + ", key2=" + key2
                    + ", behaviours=" + behaviours + "]";
        }
    }


映射的代码

    public static void main(String[] args) throws IOException  {
        String array = "[\r\n" + 
            "   {\r\n" + 
            "      \"userId\":\"11\",\r\n" + 
            "      \"otherId\":\"a\",\r\n" + 
            "      \"key1\":\"Tesla\",\r\n" + 
            "      \"key2\":\"S3\",\r\n" + 
            "      \"behaviour\":{\r\n" + 
            "         \"color\":\"white\",\r\n" + 
            "         \"size\":\"S\",\r\n" + 
            "         \"owner\":\"Mr. A\"\r\n" + 
            "      }\r\n" + 
            "   },\r\n" + 
            "   {\r\n" + 
            "      \"userId\":\"22\",\r\n" + 
            "      \"otherId\":\"\",\r\n" + 
            "      \"key1\":\"Merc\",\r\n" + 
            "      \"key2\":\"C-Class\",\r\n" + 
            "      \"behaviour\":{\r\n" + 
            "         \"color\":\"black\",\r\n" + 
            "         \"size\":\"M\",\r\n" + 
            "         \"isNew\":true\r\n" + 
            "      }\r\n" + 
            "   },\r\n" + 
            "   {\r\n" + 
            "      \"userId\":\"33\",\r\n" + 
            "      \"otherId\":\"c\",\r\n" + 
            "      \"key1\":\"Honda\",\r\n" + 
            "      \"key2\":\"CRV\",\r\n" + 
            "      \"behaviour\":{\r\n" + 
            "         \"color\":\"green\",\r\n" + 
            "         \"size\":\"L\"\r\n" + 
            "      }\r\n" + 
            "   }\r\n" + 
            "]";
        ObjectMapper mapper = new ObjectMapper();
        MainObject[] objects = mapper.readValue(array, MainObject[].class);
        System.out.println(Arrays.asList(objects));
    }

输出

    [MainObject [userId=11, otherId=a, key1=Tesla, key2=S3, behaviours={color=white, size=S, owner=Mr. A}], 
    MainObject [userId=22, otherId=, key1=Merc, key2=C-Class, behaviours={color=black, size=M, isNew=true}],
    MainObject [userId=33, otherId=c, key1=Honda, key2=CRV, behaviours={color=green, size=L}]]
英文:

Define ur class like belwo

class MainObject {
public String userId;
public String otherId;
public String key1;
public String key2;
@JsonProperty(value = &quot;behaviour&quot;)
public Map&lt;String, String&gt; behaviours;
@Override
public String toString() {
return &quot;MainObject [userId=&quot; + userId + &quot;, otherId=&quot; + otherId + &quot;, key1=&quot; + key1 + &quot;, key2=&quot; + key2
+ &quot;, behaviours=&quot; + behaviours + &quot;]&quot;;
}
}

Code to map

public static void main(String[] args) throws IOException  {
String array = &quot;[\r\n&quot; + 
&quot;   {\r\n&quot; + 
&quot;      \&quot;userId\&quot;:\&quot;11\&quot;,\r\n&quot; + 
&quot;      \&quot;otherId\&quot;:\&quot;a\&quot;,\r\n&quot; + 
&quot;      \&quot;key1\&quot;:\&quot;Tesla\&quot;,\r\n&quot; + 
&quot;      \&quot;key2\&quot;:\&quot;S3\&quot;,\r\n&quot; + 
&quot;      \&quot;behaviour\&quot;:{\r\n&quot; + 
&quot;         \&quot;color\&quot;:\&quot;white\&quot;,\r\n&quot; + 
&quot;         \&quot;size\&quot;:\&quot;S\&quot;,\r\n&quot; + 
&quot;         \&quot;owner\&quot;:\&quot;Mr. A\&quot;\r\n&quot; + 
&quot;      }\r\n&quot; + 
&quot;   },\r\n&quot; + 
&quot;   {\r\n&quot; + 
&quot;      \&quot;userId\&quot;:\&quot;22\&quot;,\r\n&quot; + 
&quot;      \&quot;otherId\&quot;:\&quot;\&quot;,\r\n&quot; + 
&quot;      \&quot;key1\&quot;:\&quot;Merc\&quot;,\r\n&quot; + 
&quot;      \&quot;key2\&quot;:\&quot;C-Class\&quot;,\r\n&quot; + 
&quot;      \&quot;behaviour\&quot;:{\r\n&quot; + 
&quot;         \&quot;color\&quot;:\&quot;black\&quot;,\r\n&quot; + 
&quot;         \&quot;size\&quot;:\&quot;M\&quot;,\r\n&quot; + 
&quot;         \&quot;isNew\&quot;:true\r\n&quot; + 
&quot;      }\r\n&quot; + 
&quot;   },\r\n&quot; + 
&quot;   {\r\n&quot; + 
&quot;      \&quot;userId\&quot;:\&quot;33\&quot;,\r\n&quot; + 
&quot;      \&quot;otherId\&quot;:\&quot;c\&quot;,\r\n&quot; + 
&quot;      \&quot;key1\&quot;:\&quot;Honda\&quot;,\r\n&quot; + 
&quot;      \&quot;key2\&quot;:\&quot;CRV\&quot;,\r\n&quot; + 
&quot;      \&quot;behaviour\&quot;:{\r\n&quot; + 
&quot;         \&quot;color\&quot;:\&quot;green\&quot;,\r\n&quot; + 
&quot;         \&quot;size\&quot;:\&quot;L\&quot;\r\n&quot; + 
&quot;      }\r\n&quot; + 
&quot;   }\r\n&quot; + 
&quot;]&quot;;
ObjectMapper mapper = new ObjectMapper();
MainObject[] objects = mapper.readValue(array, MainObject[].class);
System.out.println(Arrays.asList(objects));
}

output

 [MainObject [userId=11, otherId=a, key1=Tesla, key2=S3, behaviours={color=white, size=S, owner=Mr. A}], 
MainObject [userId=22, otherId=, key1=Merc, key2=C-Class, behaviours={color=black, size=M, isNew=true}],
MainObject [userId=33, otherId=c, key1=Honda, key2=CRV, behaviours={color=green, size=L}]]

huangapple
  • 本文由 发表于 2020年10月23日 04:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490003.html
匿名

发表评论

匿名网友

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

确定