如何在将JSON解析为映射

时间:2015-08-25 05:18:49

标签: java json jackson

我想将下面的JSON解析为POJO。我正在使用杰克逊来解析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)

4 个答案:

答案 0 :(得分:4)

1)创建一个Record类对象

2)在你不会

的字段上添加@JsonIgnore Annotation
public class Result {
    private int totalSize;
    private boolean done;
    private Record records;

[..]

}


public class Record {
    @JsonIgnore
    private Map<String, String> attributes;
    private int accountID;
    private int id;
    private int approvalID;

[..]

}

答案 1 :(得分:3)

更新2015/08/29:

正如你评论的那样

  

我通过将JSON解析为map来实现动态字段支持。忽略错误的JSON元素是待处理的

我建议您应该处理原始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();
        }

然后,使用您自己的代码achieved dynamic field support by parsing the JSON into 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

答案 2 :(得分:1)

我建议使用[Google gson API][1]'s @Expose annotation.(如果您的环境允许这样做)。

您可以简单地注释生成的json文件中所需的字段(使用@Expose),并将其保留为其他字段。在生成json期间,请使用API​​方法excludeFieldsWithoutExposeAnnotation

可以看到示例示例here

注意:在您的示例中,将您的Result视为主要POJO,records是另一个拥有attributesaccountId等的POJO领域。 然后他们之间就有了一种关系(Java composition)。

之后,你可以像下面一样调用Json到pojo转换 -

com.google.gson.Gson gson = new com.google.gson.GsonBuilder()
                    .excludeFieldsWithoutExposeAnnotation().create();

 Result result= gson.fromJson(yourjsonString, Result.class);

答案 3 :(得分:0)

如果您不想映射特定字段,则可以在字段名称上使用 @JsonIgnore 注释

public class MyJsonObj{
@JsonProperty("name")
String fieldName    

@JsonIgnore
String fieldNameIgnored;
}

如果要忽略POJO中未提及的所有字段,可以使用 在类上的 @JsonIgnoreProperties 批注

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyJsonObj{
strong text
}