常用小工具--json相关

1 json格式字符串转Map

            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
import java.util.Map;
import net.sf.json.JSONObject;

public class JsonToMap{
	
	public static void main(String[] args) {
        String json="[{\"id\":1,\"serviceId\":\"2\",\"businessTypeId\":3,\"serviceBusinessKey\":\"4\",\"businessAttribution\":\"5\",\"detailPageUrl\":\"6\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613809720000,\"updateTime\":1613809720000},{\"id\":2,\"serviceId\":\"3\",\"businessTypeId\":3,\"serviceBusinessKey\":\"5\",\"businessAttribution\":\"6\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810937000,\"updateTime\":1613810937000},{\"id\":3,\"serviceId\":\"4\",\"businessTypeId\":5,\"serviceBusinessKey\":\"6\",\"businessAttribution\":\"7\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810945000,\"updateTime\":1613810948000}]";
		json=json.replace("[", "");
		json=json.replace("]", "");
		 JSONObject jsonObject = JSONObject.fromObject(json);
	        Map<Object, Object> maps = (Map) jsonObject;
	        for (Object map : maps.entrySet()){
	            System.out.println("key:"+((Map.Entry)map).getKey()+" value:" + ((Map.Entry)map).getValue());

	        }
		
	}

}

2 json字符串转list

            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.2.4</version>
            </dependency>
import java.util.Arrays;
import java.util.List;

import com.google.gson.Gson;

public class JsonToList{

	public static void main(String[] args) {
		String jsonString="[{\"id\":1,\"serviceId\":\"2\",\"businessTypeId\":3,\"serviceBusinessKey\":\"4\",\"businessAttribution\":\"5\",\"detailPageUrl\":\"6\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613809720000,\"updateTime\":1613809720000},{\"id\":2,\"serviceId\":\"3\",\"businessTypeId\":3,\"serviceBusinessKey\":\"5\",\"businessAttribution\":\"6\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810937000,\"updateTime\":1613810937000},{\"id\":3,\"serviceId\":\"4\",\"businessTypeId\":5,\"serviceBusinessKey\":\"6\",\"businessAttribution\":\"7\",\"detailPageUrl\":\"\",\"category1Id\":0,\"category2Id\":0,\"selfSupportType\":null,\"coverImgUrl\":\"\",\"title\":\"\",\"note\":\"\",\"sellPrice\":\"0\",\"lable\":\"\",\"createTime\":1613810945000,\"updateTime\":1613810948000}]";
		Student[] array = new Gson().fromJson(jsonString,Student[].class);
		List<Student> list = Arrays.asList(array);
		Student s=new Student();
		for(int i=0;i<list.size();i++) {
			s=list.get(i);
			System.out.println(s.toString());
			
		}

	}

}
import java.util.Date;

public class Student {

    private Integer id;

    private String serviceId;

    private Integer businessTypeId;

    private String serviceBusinessKey;

    private String businessAttribution;

    private String detailPageUrl;

    private Integer category1Id;

    private Integer category2Id;

    private Integer selfSupportType;

    private String coverImgUrl;

    private String title;

    private String note;

    private String sellPrice;

    private String lable;

    private String createTime;

    private String updateTime;
	@Override
	public String toString() {
		return "Student [id=" + id + ", serviceId=" + serviceId + ", businessTypeId=" + businessTypeId
				+ ", serviceBusinessKey=" + serviceBusinessKey + ", businessAttribution=" + businessAttribution
				+ ", detailPageUrl=" + detailPageUrl + ", category1Id=" + category1Id + ", category2Id=" + category2Id
				+ ", selfSupportType=" + selfSupportType + ", coverImgUrl=" + coverImgUrl + ", title=" + title
				+ ", note=" + note + ", sellPrice=" + sellPrice + ", lable=" + lable + ", createTime=" + createTime
				+ ", updateTime=" + updateTime + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
				+ ", toString()=" + super.toString() + "]";
	}
    
    

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值