ObjectMapper 动态用法

class DymicObject {
     private Object o;

    public DymicObject(Object o) {
         this.o = o;
     }

    public DymicObject get(String s) {
         return new DymicObject(innerget(this.o, s));
     }


     public DymicObject get(int s) {
         return new DymicObject(innerget(this.o, s));
     }


     public Object Value() {
         return this.o;
     }

    private Object innerget(Object o, Object key) {
         if (o instanceof Map) {
             return ((Map) o).get(key);
         } else if (o instanceof List) {
             return ((List) o).get((Integer) key);
         }

        throw new RuntimeException("不支持");
     }





// example
  String body = message.getBody();

                ObjectMapper objectMapper = new ObjectMapper();

                Object o = objectMapper.readValue(body, Object.class);

                Object value = new DymicObject(o).get("Records").get(0).get("s3").get("bucket").get("name").Value();

posted @ 2019-04-30 14:08  张保维  阅读(533)  评论(0编辑  收藏  举报