0

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.

2
  • What does the JSON look like? Commented Jul 5, 2012 at 14:42
  • This doesn't make any sense: Your JSON is an array of (strange) strings - this will not parse with neither the code from the question, nor your own answer. Commented May 6, 2019 at 9:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.