zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JAVA中json转Map,jsonArray转List集合,List集合转json

2023-06-13 09:12:23 时间

大家好,又见面了,我是你们的朋友全栈君。

在写代码时,经常会遇到各转类型之间互相转换,比如json转换为Map,jsonArray转List集合,List集合转json,现在整理一个工具类,方便日后查阅。

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.zgr.pack.entity.test.TestJsonToList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


public class Util {
	    //json字符串转换为MAP
		public static Map jsonStrToMap(String s) {
			Map map = new HashMap();
			//注意这里JSONObject引入的是net.sf.json
			net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(s);
			Iterator keys = json.keys();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				String value = json.get(key).toString();
				if (value.startsWith("{") && value.endsWith("}")) {
					map.put(key, jsonStrToMap(value));
				} else {
					map.put(key, value);
				}

			}
			return map;
		}
		
		// 将jsonArray字符串转换成List集合
		public static List jsonToList(String json, Class beanClass) {
			if (!StringUtils.isBlank(json)) {
				//这里的JSONObject引入的是 com.alibaba.fastjson.JSONObject;
				return JSONObject.parseArray(json, beanClass);
			} else {
				return null;
			}
		}
		
		//List集合转换为json
		public static JSON listToJson(List list) {
			JSON json=(JSON) JSON.toJSON(list);
			return json;
		}
		
		
		public static void main(String[] args) {
			
			System.out.println("---------------------json字符串转换为MAP---------------------");
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("abc", 123);
			jsonObject.put("def", 456);
			System.out.println("A==========json====="+jsonObject);
			Map map=Util.jsonStrToMap(jsonObject.toString());
			System.out.println("B==========def======"+map.get("def"));
			
			
			System.out.println("---------------------将jsonArray字符串转换成List集合---------------------");
			String str="[{\"year\":\"2015\",\"month\":10,\"count\":47},{\"year\":2017,\"month\":12,\"count\":4}]";
			//这里需要指定泛型,我们建立一个实体类TestJsonToList
			List<TestJsonToList> list=Util.jsonToList(str, TestJsonToList.class);
			System.out.println("C==========取list第二个元素的year====="+list.get(1).getYear());
			
			
			System.out.println("---------------------将list集合转换成json---------------------");
			//这里的JSONObject引入的是 com.alibaba.fastjson.JSONObject;
			JSON json=Util.listToJson(list);
			System.out.println("D==========json====="+json);
		}
}

实体类

public class TestJsonToList {
	private String year;      //年
    private String month;     //月
    private String count;     //数据
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getCount() {
		return count;
	}
	public void setCount(String count) {
		this.count = count;
	}
	
    //构造方法
    public TestJsonToList(String year, String month, String count) {  
        this.year = year;  
        this.month = month;  
        this.count = count;  
    }  
    //默认构造方法
    public TestJsonToList() {  
      
    }  
}

控制吧输出结果:

json转List集合,和List集合转json时需要注意,使用的是阿里的fastJson.jar包,不要引错了,Maven项目对应引入:

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

需要jar包,可去这里下载:

http://download.csdn.net/download/weixin_33446857/10225429

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152270.html原文链接:https://javaforall.cn