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();

腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/SElwbJnj7FM8OXH2 (转载请保留)。
关注下面的标签,发现更多相似文章