Java-> fastjson的使用实例

 fastjson 是实体对象转Json的格式的工具jar,是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致。它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中

<!--jar 依赖-->
<dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
            <version>1.2.30</version>  
</dependency>  

实例1: 


import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class Fastjson {
	public static void main(String[] args) {
		People people=new People();
		people.setAge(120);
		people.setName("xixixi");
		List<People> list=new ArrayList<People>();
		list.add(people);
		list.add(people);
		System.out.println(JSON.toJSON(people));
		System.out.println(JSONObject.toJSON(people));
		System.out.println(JSON.toJSON(list));
		String peoplejson="{'name':'hheheheh','age':13}";
		People p=JSONObject.parseObject(peoplejson, People.class);
		System.out.println(p.getAge());
		//String listjson="[{'name':'heheh','age':22}]";*/
	}
}




public class People {
	private String name;
	private int age;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}


}
输出:
{"name":"xixixi","age":120}
{"name":"xixixi","age":120}
[{"name":"xixixi","age":120},{"name":"xixixi","age":120}]
13

实例2

public static void main(String[] args) throws Throwable
	{
		List<Picture> lits = new ArrayList<>();
		Picture p1 = new Picture("xiaoming", null, DateUtil.toDate("2019-05-13", "yyyy-MM-dd"), 1);
		Picture p2 = new Picture("xiaohong", null, DateUtil.toDate("2019-05-13", "yyyy-MM-dd"), 1);
		lits.add(p1);
		lits.add(p2);
		System.out.println( DateUtil.toString(new Date(), "yyyy-MM-dd"));
		//为null 的不会输出
		String jsonString =JSON.toJSONString(lits);
		System.out.println(jsonString);
		//为null 的也会输出
		String jsonString1=JSON.toJSONString(lits,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
		System.out.println(jsonString1);
	}

//----------------------------------------------

class Picture {
	private Integer pic_id;
	private String pic_name;
	private String pic_url;
	private String pic_type;
	private Date pic_ctime;
	private Date pic_ltime;
	private String pic_intro;
	private Integer state;
	
	public Picture(){}
	
	public Picture(String pic_name, String pic_url,Date pic_ltime,Integer state) {
		this.pic_name = pic_name;
		this.pic_url = pic_url;
		this.pic_ltime = pic_ltime;
		this.state = state;
	}
    ...................................
}

输出:

2019-05-13
[{"pic_ltime":1557676800000,"pic_name":"xiaoming","state":1},{"pic_ltime":1557676800000,"pic_name":"xiaohong","state":1}]
[{"id":0,"pic_ctime":null,"pic_intro":"","pic_ltime":1557676800000,"pic_name":"xiaoming","pic_type":"","pic_url":"","state":1},{"id":0,"pic_ctime":null,"pic_intro":"","pic_ltime":1557676800000,"pic_name":"xiaohong","pic_type":"","pic_url":"","state":1}]

如果不使用创建实体如下:

JSONObject object =JSON.parseObject("JSon格式字符串");

实例3:获取json 字符串中某个值:


String jsonStr="{'code':100,'data':{'name':'小明','age':16}}";

//先把String对象转换成json对象
JSONObject object= JSONObject.parseObject(jsonStr);

System.out.println(object.get("code").toString()));
System.out.println(object.getJSONObject("data").get("name"));
 

更多可参考:java中string与json互相转化_jsonstring是什么_a55650892的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值