【问题标题】:Parsing JSON with Jackson Java使用 Jackson Java 解析 JSON
【发布时间】:2012-07-05 23:40:29
【问题描述】:

我在使用 Jackson 解析 JSON 时遇到问题。我有一个 POJO 对象,被另一个对象包裹。

这是我的代码:

in main:
ObjectMapper mapper = new ObjectMapper();

List<ItemBean> mpl2 = mapper.readValue(col.toString(),new TypeReference<List<ItemBean>>() {});
my POJO class:

public class ItemBean implements Serializable {
    private List<Item> items;
    @JsonProperty("Item")
    public List<Item> getItems() {
        return items;
    }
    public void setItems(List<Item> items) {
        this.items = items;
    }
}

public class Item implements Serializable{
    public String field1;
    public Integer field2;

    public static final class Field3 extends GenericJson {
        private String subfield1;
        private String subfield2;
    }
}

这里是抛出的异常:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "item" (Class bean.item), not marked as ignorable
 at [Source: java.io.StringReader@101b6d56; line: 4, column: 16] (through reference chain: bean.ItemBean["items"]->bean.Item["item"])

JSON 看起来是这样的:

["{\n
            \"items\":
        [
        \n  {
        \n \"item\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                         \n      \"subfield2\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },
        \n \"item\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                         \n      \"subfield2\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },
        \n \"item\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                         \n      \"subfield2\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },


etc......   may I haven't closed brackets correctly, but they are correct :)
            }
        ]
    "]

POJO 完全重复 JSON 对象的字段。

【问题讨论】:

  • JSON 是什么样的?
  • 这没有任何意义:你的 JSON 是一个(奇怪的)字符串数组 - 这不会用问题中的代码或你自己的答案来解析。

标签: java json string parsing jackson


【解决方案1】:

我写了自己的方法,解析这种结构的 JSON。

代码如下:

public static List parseList(String jsonInput, Class clazz) {
    List result = new LinkedList();
    JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonInput);
    JSONObject items = (JSONObject)json.getJSONObject(0);
    JSONArray dataArrayJSON = (JSONArray)items.getJSONArray("items");

    for (int i = 0; i < dataArrayJSON.size(); i++) {
        result.add(JSONObject.toBean(dataArrayJSON.getJSONObject(i).getJSONObject("item"), clazz));
    }
    return result;
}

问题是项目在数组中,而项目是唯一的元素。 items 反过来也是一个数组,因此我使用了dataArrayJSON.getJSONObject(i).getJSONObject("item") 构造。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2014-06-11
    相关资源
    最近更新 更多