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

本文详细介绍如何使用Java进行JSON数据的转换,包括将JSON字符串转换为Map、将JSONArray字符串转换为List集合,以及将List集合转换回JSON。文章通过具体代码示例展示了如何利用阿里的fastjson库实现这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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.youkuaiyun.com/download/weixin_33446857/10225429
--------------------- 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值