Deserializing json with various keys as value

时间:2015-11-12 11:33:22

标签: java json jackson

I have json like:

{"avatars": {
 "1": "value",
 "2":"value",
 "900":"value"
 }
}

And my model:

class Response{
 List<Avatar> avatars;
}

class Avatar{
 String id;
 String value;
}

How do I properly parse the Json using Jackson

3 个答案:

答案 0 :(得分:1)

You should use json like this to automaticaly parse:

{"avatars": [
 {"id": "1", "value": "someValue1"},
 {"id": "2", "value": "someValue2"},
 {"id": "300", "value": "someValue300"},
 ]
} 

or write custom parser for Jackson.

答案 1 :(得分:0)

Try this:

Using Java JSON library

public class Test {
    public static void main(String[] args) {
        Response response = new Response();
        Serializer.serialize("{\"avatars\": { \"1\": \"value\", \"2\":\"value\", \"900\":\"value\" }}", response);

        System.out.println(response.toString());
    }
}

class Serializer {
    public static void serialize(String j, Response response) {
        try {

            JSONObject json = new JSONObject(j).getJSONObject("avatars");
            Iterator keys = json.keys();

            while (keys.hasNext()) {
                String id = keys.next().toString();
                String value = json.getString(id);
                response.addAvatar(id, value);
            }
        } catch (JSONException ignore) {

        }
    }
}

/**
 * This is a response class
 */
class Response {
    List<Avatar> avatars;

    public Response() {
        /**
         * You can use LinkedList, I think it's the best way.
         */
        this.avatars = new LinkedList<Avatar>();
    }

    public void addAvatar(String id, String value) {
        this.avatars.add(new Avatar(id, value));
    }

    public String toString() {
        String result = "";
        for (Avatar avatar : this.avatars) {
            result += (result.length() == 0 ? "" : ", ") + "[" + avatar.getId() + "=" + avatar.getValue() + "]";
        }

        return result;
    }
}

/**
 * This is an avatar class
 */
class Avatar {
    private String id;
    private String value;

    public Avatar(String id, String value) {
        this.id = id;
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Hope this helps!

答案 2 :(得分:0)

您可以使用转换器,这可以避免完整自定义反序列化器的复杂性:

    @JsonDeserialize(converter = AvatarMapConverter.class)
    public List<Avatar> avatars;

转换器需要声明它可以接受Jackson可以反序列化的其他类型,并生成List<Avatar>。扩展StdConverter将为您完成管道工程:

public class AvatarMapConverter extends StdConverter<Map<String, String>, List<Avatar>> {
    @Override
    public List<Avatar> convert(Map<String, String> input) {
        List<Avatar> output = new ArrayList<>(input.size());
        input.forEach((id, value) -> output.add(new Avatar(id, value)));
        return output;
    }
}

如果您还需要序列化,您可以编写转换器以反向转换并从@JsonSerialize注释引用该转换器。

相关问题