Parsing JSON with Jackson Java

后端 未结 1 516
面向向阳花
面向向阳花 2021-01-28 02:54

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 ObjectMap         


        
相关标签:
1条回答
  • 2021-01-28 03:25

    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.

    0 讨论(0)
提交回复
热议问题