java解析json数据

这篇博客介绍了如何在Java中通过Maven引入阿里巴巴的Fastjson库,并展示了如何解析包含ID和数据列表的JSON对象。首先,添加Fastjson的依赖到项目,然后通过Fastjson的方法获取JSON对象的ID和转换数据数组为UserBean对象列表。
摘要由CSDN通过智能技术生成

1、maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.70</version>
</dependency>

2、JSON格式

{ "id":"UUID" "data":[ {"name":"zhangsan","sex":"1"}, {"name":"lisi","sex":"2"} ] }

 

3、解析(传入参数jsonObject)

Object id = jsonObject.get("id");

JSONArray jsonArray = jsonObject.getJSONArray("data");

LIst<UserBean> list= jsonArray.toJavaList(UserBean.class);

java解析json字符串。 commons-beanutils-1.9.0 commons-collections-3.2.1 commons-lang-2.6 commons-logging-1.1.3 ezmorph-1.0.6 json-lib-2.4-jdk15 demo: package com; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.ezmorph.object.DateMorpher; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; public class Jsontest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "hzj"); jsonObj.put("sex", "female"); System.out.println(jsonObj); } public static Object jsonToBean(String jsonString, Class cla) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla); } public static Object jsonToBean(String jsonString, Class cla, Map map) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla, map); } public static Object[] jsonToArray(String jsonString, Class cla) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static Object[] jsonToArray(String jsonString, Class cla, Map map) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla, map); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static List jsonToList(String jsonString, Class cla) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static List jsonToList(String jsonString, Class cla, Map map) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla, map)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static Map jsonToMap(String jsonString) { Map map = null; try { setDateFormat2Java(); JSONObject jsonObject = JSONObject.fromObject(jsonString); map = new HashMap(); for (Iterator iter = jsonObject.keys(); iter.hasNext();) { String key = (String) iter.next(); map.put(key, jsonObject.get(key)); } } catch (Exception ex) { ex.printStackTrace(); } return map; } public static Object[] jsonToArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); } public static void setDateFormat2Java() { JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" })); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值