John Smith
John Smith

Reputation: 851

Parsing JSON with Jackson Java

I have a problem while parsing JSON with Jackson. I have a POJO object, wrapped by another.

Here is my code:

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;
    }
}

And here is the thrown exception:

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 looks in a such way:

["{\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 totally repeat the fields of the JSON object.

Upvotes: 0

Views: 3747

Answers (1)

John Smith
John Smith

Reputation: 851

I wrote my own method, which parses JSON of such a structure.

Here is the code:

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;
}

The problem was that items are in the array and items is the only element. items in its turn, is also an array, thus I used the dataArrayJSON.getJSONObject(i).getJSONObject("item") construction.

Upvotes: 1

Related Questions