码迷,mamicode.com
首页 > Web开发 > 详细

fastjson 的简单使用

时间:2016-02-18 17:52:58      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

	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;
	}
}


fastjson 的简单使用

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5198713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!