java的json解析工具_json文件解析工具类(java)

附上java解析json文件内容的工具类

1.工具类

import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**

* @ClassName CommonUtil

* @Description TODO

* @Author lifay

* @Date 2020/2/18 10:21

**/

public class CommonUtil {

private static Logger logger = LoggerFactory.getLogger(CommonUtil.class);

/**

* 读取json文件返回数据

* @throws IOException

*/

public static Object readJsonData(String filePath,Class clazz){

String jsonString = null;

Reader reader = null;

try{

ClassPathResource resource = new ClassPathResource(filePath);

if (!resource.exists()) {

logger.warn("{}文件不存在!",filePath);

return null;

}

File file = resource.getFile();

reader = new InputStreamReader(new FileInputStream(file),"utf-8");

int ch = 0;

StringBuffer sb = new StringBuffer();

while ((ch = reader.read()) != -1) {

sb.append((char) ch);

}

reader.close();

jsonString = sb.toString();

} catch (IOException e){

logger.warn("{}文件内容解析失败!",filePath);

return null;

}

return JSONObject.parseObject(jsonString,clazz);

}

}

2.调用

List users = (List) CommonUtil.readJsonData("/json/user.json", List.class);

3.json文件(json文件放在resources目录下,结构为/src/main/resources/json/user.json)

[{"id":"1","name":"user1","age":"18"},{"id":"2","name":"user2","age":"28"}]

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、付费专栏及课程。

余额充值