• fastjson 的简单使用


    	public static void main(String[] args) {
    		/*普通对象与json相互转换*/
    		User u = new User("miquan", "000");
    		
    		//{"@type":"testjava.User","password":"000","userName":"miquan"}
    		String jsonUser = JSON.toJSONString(u, SerializerFeature.WriteClassName);
    		System.out.println(jsonUser);
    		
    		User user = JSON.parseObject(jsonUser, User.class);
    		
    		/*带集合的对象与json字符串相互转换*/
    		List<String> list = new ArrayList<String>();
    		list.add("miquan");
    		list.add("qiantu");
    		Download d = new Download();
    		d.setEmojiSetName("guoqiao");
    		d.setEmojiPathList(list);
    		
    		//{"@type":"testjava.Download","emojiPathList":["miquan","qiantu"],"emojiSetName":"guoqiao"}
    		String jsonList = JSON.toJSONString(d, SerializerFeature.WriteClassName);
    		System.out.println(jsonList);
    		
    		Download download = JSON.parseObject(jsonList, Download.class);
    		
    		/*json与List对象相互转换*/		
    		List<User> listUser = new ArrayList<User>();
    		listUser.add(new User("liangguoqiao", "iloveyou"));
    		listUser.add(new User("liangguoqiao", "iloveyou"));
    		
    		//[{"password":"iloveyou","userName":"liangguoqiao"},
    		//{"password":"iloveyou","userName":"liangguoqiao"}]
    		String json = JSON.toJSONString(listUser);
    		System.out.println(json);
    		
    		List<User> users = JSON.parseArray(json, User.class);
    	}


    注意:1、下载地址

        2、User和Download等实体类必须加入空的构造函数

                3、转为JSON字符串的时候加上SerializerFeature.WriteClassName

                4、坐等下班。。。


    package com.yangshidesign.weixinface.bean;
    
    import java.util.List;
    
    public class Download {
    	private String emojiSetName;
    	/** 表情图片名字 */
    	private List<String> emojiNameList;
    	
    	public Download() {
    		super();
    	}
    	public String getEmojiSetName() {
    		return emojiSetName;
    	}
    	public void setEmojiSetName(String emojiSetName) {
    		this.emojiSetName = emojiSetName;
    	}
    	public List<String> getEmojiNameList() {
    		return emojiNameList;
    	}
    	public void setEmojiNameList(List<String> emojiNameList) {
    		this.emojiNameList = emojiNameList;
    	}
    }
    

    package testjava;
    
    public class User {
    	private String userName;
    	private String password;
    	
    	public User() {
    		super();
    	}
    	public User(String userName, String password) {
    		super();
    		this.userName = userName;
    		this.password = password;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }
    


  • 相关阅读:
    分布式机器学习:算法、理论与实践——【1】
    LLVM Cookbook
    【前端】Webpack 进阶
    Noip2015 运输计划 树上差分 二分答案
    bzoj 2259: [Oibh]新型计算机 最短路 建模
    888E
    [ZJOI2012]旅游 对偶图 树的直径
    [HAOI2007]理想的正方形 单调队列 暴力
    bzoj1457: 棋盘游戏 SG函数 Nim
    Bomb HDU
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5198713.html
Copyright © 2020-2023  润新知