In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to parse the conversion between json and bean in the tool class CommonJsonUtils, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Package com.xmdishi.fmp.utils;import net.sf.json.util.JSONUtils;import org.apache.commons.lang3.StringUtils;import java.util.* / * conversion between json and bean * chenjianquan * 2019-1-14 * / public class CommonJsonUtils {/ * json to List * @ author cjianquan * @ date 2020-1-19 * @ param [json, clazz] * @ return java.util.List * / public List toBeanList (String json,Class clazz) {/ / call DateMorpherEx DefaultValue is null JSONUtils.getMorpherRegistry () .registerMorpher (new DateMorpherEx (new String [] {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}, (Date) null)) Net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject (json); List list = new ArrayList (); for (int I = 0; I < jsonArr.size (); iTunes +) {Object obj = jsonArr.get (I); T rec = (T) net.sf.json.JSONObject.toBean (net.sf.json.JSONObject.fromObject (obj), clazz); list.add (rec) } return list } / * json contains a subarray of this key, converted to List * @ author cjianquan * @ date 2020-1-19 * @ param [json, key, clazz] * @ return java.util.List * / public List toBeanListByKey (String json,String key,Class clazz) {/ / call DateMorpherEx DefaultValue is null JSONUtils.getMorpherRegistry () .registerMorpher (new DateMorpherEx (new String [] {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}, (Date) null)) List mapList = getMapListByKey (json,key); net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject (mapList); List list = new ArrayList (); for (int I = 0; I < jsonArr.size (); iTunes +) {Object obj = jsonArr.get (I); T rec = (T) net.sf.json.JSONObject.toBean (net.sf.json.JSONObject.fromObject (obj), clazz) List.add (rec);} return list;} / * convert json to List * @ author cjianquan * @ date 2020-1-19 * @ param [json] * @ return java.util.List * / public List toMapList (String json) {net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject (json) List list = new ArrayList (); for (int I = 0; I < jsonArr.size (); iTunes +) {net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject (jsonArr.get (I)); Iterator iterable= obj.keys (); Map map = new HashMap (); while (iterable.hasNext ()) {Object key = iterable.next () Map.put ("" + key,obj.get (key));} list.add (map);} return list } / * json contains a subarray of this key, converted to List * @ author cjianquan * @ date 2020-1-19 * @ param [json, key] * @ return java.util.List * / public List getMapListByKey (String json,String key) {List list = new ArrayList (); net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject (json) Object objList = params.get (key); list = toMapList (objList.toString ()); return list;} / * get the Map * @ author cjianquan * @ date of the specified key of json on 2020-1-19 * @ param [json, key] * @ return java.util.Map * / public Map getMapByKey (String json,String key) {Map map = new HashMap () Net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject (json); Object objMap = params.get (key); map = net.sf.json.JSONObject.fromObject (objMap); return map } / * obtain the Map of the specified key of json and convert it to T * @ author cjianquan * @ date 2020-1-19 * @ param [json, key, clazz] * @ return T * / public T toBeanByKey (String json, String key, Class clazz) {JSONUtils.getMorpherRegistry (). RegisterMorpher (new DateMorpherEx (new String [] {"yyyy-MM-dd HH:mm:ss") "yyyy-MM-dd"}, (Date) null)) Net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject (json); Map map = getMapByKey (json,key); T rec = (T) net.sf.json.JSONObject.toBean (net.sf.json.JSONObject.fromObject (map), clazz); return rec } / * get the json string * @ author cjianquan * @ date of the specified key of json 2020-1-19 * @ param [json, key] * @ return java.lang.String * / public String getStringByKey (String json,String key) {net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject (json); Object result = params.get (key); return "" + result } public T toBean (String json, Class clazz) {JSONUtils.getMorpherRegistry () .registerMorpher (new DateMorpherEx (new String [] {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}, (Date) null)); T rec = (T) net.sf.json.JSONObject.toBean (net.sf.json.JSONObject.fromObject (json), clazz); return rec }} the answer to the question on how to parse the conversion between json and bean in the tool class CommonJsonUtils is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.