gpt4 book ai didi

java - 如何在将 JSON 解析为 map 时忽略特定字段

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:34 25 4
gpt4 key购买 nike

我想将下面的 JSON 解析为 POJO。我正在使用 jackson 来解析 json。

{
"totalSize": 4,
"done": true,
"records": [
{
"attributes": {
"type": "oppor",
"url": "/service/oppor/456"
},
"AccountId": "123",
"Id": "456",
"ProposalID": "103"
}
]
}

在上面的JSON中,字段“totalSize”、“done”、“records”和“attributes”都是已知字段。而“AccountId”、“Id”和“ProposalID”是未知字段。在上面的 JSON 中,我不需要“属性”成为我的 bean 对象的一部分。

这是我的 JSON 的等效 bean 类

public class Result {
private int totalSize;
private boolean done;
private List<Map<String, String>> records;

public int getTotalSize() {
return totalSize;
}

public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}

public boolean isDone() {
return done;
}

public void setDone(boolean done) {
this.done = done;
}

public List<Map<String,String>> getRecords() {
return records;
}

public void setRecords(List<Map<String, String>> records) {
this.records = records;
}

}

因此记录元素中有未知字段 我刚刚使用 List 获取 bean 中的结果元素。在此 map 中,我不需要“属性”字段。解析时如何忽略它?下面是我得到的异常,因为属性不是字符串元素。

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: [B@66fdec9; line: 1, column: 40] (through reference chain: com.sample.json.Result["records"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:430)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:312)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)

最佳答案

2015 年 8 月 29 日更新:

正如你所说

I achieved dynamic field support by parsing the JSON into map. Ignoring bad JSON element is what pending

我建议您应该处理原始 JSONObject 以从中删除 "attributes" 元素。

原始的JSONObject,例如:

{
"totalSize": 4,
"done": true,
"records": [
{
"attributes": {
"type": "oppor",
"url": "/service/oppor/456"
},
"AccountId": "123",
"Id": "456",
"ProposalID": "103"
}
]
}

经过处理后,新的 JSONObject 将如下所示:

{
"records": {
"AccountId": "123",
"Id": "456",
"ProposalID": "103"
},
"totalSize": 4,
"done": true
}

使用如下代码:

        JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonString1);
JSONArray jsonArray = new JSONArray(jsonObject.get("records").toString());
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
jsonObject1.remove("attributes");
jsonObject.put("records", jsonObject1);
} catch (JSONException e) {
e.printStackTrace();
}

然后,使用您自己的代码通过将 JSON 解析为 map 来实现动态字段支持

更新结束 2015/08/29

我建议你在这种情况下使用Gsontransient

像这样

        String jsonString1 = "{\n" +
" \"totalSize\": 4,\n" +
" \"done\": true,\n" +
" \"records\": [\n" +
" {\n" +
" \"attributes\": {\n" +
" \"type\": \"oppor\",\n" +
" \"url\": \"/service/oppor/456\"\n" +
" },\n" +
" \"AccountId\": \"123\",\n" +
" \"Id\": \"456\",\n" +
" \"ProposalID\": \"103\"\n" +
" }\n" +
" ]\n" +
"}";

Gson gson = new Gson();
Result result1 = gson.fromJson(jsonString1, Result.class);

你的类,注意transient:

public class Result {
private int totalSize;
private boolean done;
private List<Record> records;
}

public class Record {
private transient Map<String, String> attributes;
private int AccountId;
private int Id;
private int ProposalID;
}

你会得到结果:

enter image description here

P/S:我在 Android Studio 中测试过 :)

更新:

      String jsonString1 = "{\n" +
" \"totalSize\": 4,\n" +
" \"done\": true,\n" +
" \"records\": [\n" +
" {\n" +
" \"attributes\": {\n" +
" \"type\": \"oppor\",\n" +
" \"url\": \"/service/oppor/456\"\n" +
" },\n" +
" \"AccountId\": \"123\",\n" +
" \"Id\": \"456\",\n" +
" \"ProposalID\": \"103\"\n" +
" }\n" +
" ]\n" +
"}";
Gson gson = new Gson();
Object object = gson.fromJson(jsonString1, Object.class);
Map<String, String> stringMap = (Map<String, String>) object;
Result myResult = new Result();
Iterator entries = stringMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
switch (key) {
case "totalSize":
myResult.totalSize = (int) Double.parseDouble(entry.getValue().toString());
break;
case "done":
myResult.done = Boolean.valueOf(entry.getValue().toString());
break;
case "records":
try{
Object object1 = entry.getValue();
List<Object> objectList = (List<Object>) object1;
Map<String, Object> stringMap2 = (Map<String, Object>) objectList.get(0);
Map<String, String> recordMap = new HashMap<>();
Iterator entries2 = stringMap2.entrySet().iterator();
while (entries2.hasNext()) {
Map.Entry entry2 = (Map.Entry) entries2.next();
String key2 = entry2.getKey().toString();
String value2 = entry2.getValue().toString();
if (!"attributes".equals(key2)) {
recordMap.put(key2, value2);
}
entries2.remove();
}
myResult.records = recordMap;
} catch (Exception e) {
e.printStackTrace();
}
break;
}
entries.remove();
}

类:

public class Result {
private int totalSize;
private boolean done;
private Map<String, String> records;
}

调试结果:

enter image description here

关于java - 如何在将 JSON 解析为 map 时忽略特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com