zoukankan      html  css  js  c++  java
  • JsonObject、JsonArray操作json的个人总结

    介绍 JsonObject、JsonArray之前,先介绍下JsonConfig

    JsonConfig:

    1. setClassMap(Map classMap)
      设置json属性类型,上json里的其中值为List时,可通过此属性设置属性类型
      Map jMap = new HashMap();
      jMap.put("contactPersonList",ContactPerson.class);
      jMap.put("subscribedList",InvestorSubscribed.class);
      jsonConfig.setClassMap(jMap);
    2. setRootClass(Class rootClass)
      设置json要转化的java类型
    3. setExcludes(String[] excludes)
      去除不需要转化的属性
    4. setJavaPropertyFilter(PropertyFilter javaPropertyFilter)
      通过实现PropertyFilter 的apply方法来去除不需要转化的属性。当返回true时,则过滤(其中ower为当前实体类,name为属性,value为值)
       jsonConfig.setJavaPropertyFilter((ower,name,value) -> {
                  return true;
        });

    JsonObject常用方法:

    1. Object toBean(JSONObject jsonObject)
    2. Object toBean(JSONObject jsonObject, Class beanClass)
      beabClass既是设置JsonConfigde的RootClass
    3. Object toBean(JSONObject jsonObject, Class beanClass, Map classMap)
    4. Object toBean(JSONObject jsonObject, JsonConfig jsonConfig)

    JsonArray常用方法:

    1. Collection toCollection(JSONArray jsonArray)
    2. Collection toCollection(JSONArray jsonArray, Class objectClass)
    3. Collection toCollection(JSONArray jsonArray, JsonConfig jsonConfig)

    将java类转为Json时常用方法:

    1.  void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)//注意一个是jsonProperty,一个是javaProperty
    2. void registerJsonValueProcessor(Class propertyType, JsonValueProcessor jsonValueProcessor)
      此方法为遇到 制定类型时,进行数据转化格式,常用语转化Date时间格式。实现JsonValueProcessor的
      Object processArrayValue(Object var1, JsonConfig var2)//处理单个类
      Object processObjectValue(String var1, Object var2, JsonConfig var3)//处理数组的类
      ps:注意对null进行判断
  • 相关阅读:
    [poj] 3068 "Shortest" pair of paths || 最小费用最大流
    [poj] 3686 The Windy's || 最小费用最大流
    [poj] 1235 Farm Tour || 最小费用最大流
    [poj] 3281 Dining || 最大流
    [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配
    luogu P1072 Hankson 的趣味题
    二分图最佳匹配
    181106 solution
    luogu P2216 [HAOI2007]理想的正方形
    luogu P4362 [NOI2002]贪吃的九头龙
  • 原文地址:https://www.cnblogs.com/jaxlove-it/p/7660602.html
Copyright © 2011-2022 走看看