public static List<ZhiBiaoObj> analysisJson(String json) {
        List<ZhiBiaoJsonObj> zhiBiaoJsonObjs = JSONArray.parseArray(json, ZhiBiaoJsonObj.class);
        List<ZhiBiaoObj> resultList = new ArrayList<>();
        if (!zhiBiaoJsonObjs.isEmpty()) {
            getList(zhiBiaoJsonObjs, resultList);
        // 根据id正序
        resultList.sort(new Comparator<ZhiBiaoObj>() {
            @Override
            public int compare(ZhiBiaoObj o1, ZhiBiaoObj o2) {
                return o1.getId() - o2.getId();
        return resultList;
    private static List<ZhiBiaoObj> getList(List<ZhiBiaoJsonObj> zhiBiaoJsonObjs, List<ZhiBiaoObj> resultList) {
        for (ZhiBiaoJsonObj item : zhiBiaoJsonObjs) {
            ZhiBiaoObj zhiBiaoObj = new ZhiBiaoObj();
            BeanUtils.copyProperties(item, zhiBiaoObj); // 对象属性名相同时值复制  目标对象/源对象
            List<ZhiBiaoJsonObj> children = item.getChildren();
            if (!children.isEmpty()) {
                getList(children, resultList);
            if (!resultList.contains(zhiBiaoObj)) {
                resultList.add(zhiBiaoObj);
        return resultList;
                    Json:[{	"id": 1,	"parentId": 0,	"name": "自然资源概况",	"updateFrequency": "年度",	"source": "上报数据",	"children": [{		"id": 2,		"parentId": 1,		"name": "土地资源",		"updateFrequency": "年度",		"source": "上报数据",		"children": [{			"id": 3,			"parentId": 2,
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" }));
				
1,什么是 JSONjson的全称为:JavaScript Object Notation,是一种轻量级的数据交互格式。 采用完全独立于编程语言的文本格式来存储和表示数据 背景:数据传输是我们在敲代码时,经常遇到的一个场景,前后端交互。给数据一个统一的格式有利于我们编写和解析数据。 2,JSON的格式 json中含有两种复合类的值:数组、对象 基本类的值有四种:字符串、数值(必须以十进制表示)、布尔值、null 字符串必须使用双引号表示,不能使用单引号。 对象的键名最好放在双引号里面。
在网站开发中经常遇到级联数据的展示,比如选择城市的时候弹出的省市县选择界面。很多前端制作人员习惯于从JSON中而不是从数据库中获取省市县数据。那么在选择了省市县中的某一个城市 ,存储到数据库中需要存储所选城市的代码。所以需要一个能将JSON数据(一般存储在javascript脚本中)结构全部导入到数据库中的功能。 JSON的特点是支持层级结构、支持数组表示的对象 。下面的示例介绍如何将JSON...
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; JSONObject jsonObject = JSON.parseObject(jsonString); String name = jsonObject.getString("name"); int age = jsonObject.getIntValue("age"); String city = jsonObject.getString("city"); 其中,jsonString是要转换的JSON字符串,JSONObject是FastJson库中的一个类,可以将JSON字符串转Java对象。使用getString、getIntValue等方法可以获取JSON中的属性值。