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

JSON初体验(三):FastJson解析

时间:2017-06-15 21:49:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:体验   api   步骤   ast   pen   ret   div   add   解析json数据   

JSON解析之FastJson(阿里巴巴解析开源)
  特点:
    Fastjson是一个Java语言编写的高性能功能完善的JSON库,它采用的
    是一种"假定有序快速匹配"的算法,把JSON Parse的性能提升到极致,
    是目前Java语言中最快的JSON库。

  1.将json格式{}转换为Java对象
    API:
      <T> T parseObject(String json,Class<T> classOfT);
    步骤:
      1.导入fastjson的jar包
      2.JSON调用parseObject()方法,获取转换后的java对象
      例如:
        ShopInfo shopInfo=JSON.parseObject(json,ShopInfo.class);

    代码:

    ShopInfo的类:

    

public class ShopInfo {
  private int id;
  private String name;
  private double price;
  private String imagePath;

  public ShopInfo() {
  }

  public ShopInfo(int id, String name, double price, String imagePath) {
	this.id = id;
	this.name = name;
	this.price = price;
	this.imagePath = imagePath;
  }

  public int getId() {
	return id;
  }

  public void setId(int id) {
	this.id = id;
  }

  public String getName() {
	return name;
  }

  public void setName(String name) {
	this.name = name;
  }

  public double getPrice() {
	return price;
  }

  public void setPrice(double price) {
	this.price = price;
  }

  public String getImagePath() {
	return imagePath;
  }

  public void setImagePath(String imagePath) {
	this.imagePath = imagePath;
  }

  @Override
  public String toString() {
	return "ShopInfo{" +
	  "id=" + id +
	  ", name=‘" + name + ‘\‘‘ +
	  ", price=" + price +
	  ", imagePath=‘" + imagePath + ‘\‘‘ +
	  ‘}‘;
  }
}

  解析json数据:

public class One {
  public static void main(String[] args) {
	String json = "{\n" +
		"    \"id\":1,\n" +
		"    \"name\":\"豆瓣\",\n" +
		"    \"price\":12.3,\n" +
		"    \"imagePath\":\"http://www.douban.com\"\n" +
		"}";
	ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
	System.out.println(shopInfo.toString());
  }
}

  

  2.将json格式的字符串[]转换为Java对象的List
    API:
      List<T> parseArray(String json,Class<T> classOft);
    步骤:
      1.导入fastjson的jar包
      2.JSON调用parseArray()方法,获取转换后的java集合
      例如:
        List<ShopInfo> shopInfos = JSON.parseArray(json,ShopInfo.class);
    代码:

public class Two {
  public static void main(String[] args) {
	String json = "[\n" +
		"    {\n" +
		"        \"id\":1,\n" +
		"        \"imagePath\":\"http://www.baidu.com\",\n" +
		"        \"name\":\"大虾1\",\n" +
		"        \"price\":12.3\n" +
		"    },\n" +
		"    {\n" +
		"        \"id\":2,\n" +
		"        \"imagePath\":\"http://www.douban.com\",\n" +
		"        \"name\":\"大虾2\",\n" +
		"        \"price\":12.5\n" +
		"    }\n" +
		"]";
	List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
	System.out.println(shopInfos);
  }
}

 

  3.将java对象转换为json字符串{}
    API:
      String toJSONString(Object object);
    步骤:
      1.导入fastjson的jar包
      2.JSON调用toJSONString()方法,获取转换后的json数据
      例如:
        ShopInfo shopInfo = new ShopInfo(1,"鲍鱼",255.5,"baoyu");
        String json = JSON.toJSONString(shopInfo);
    代码:

  

public class Three {
  public static void main(String[] args) {
	ShopInfo shopInfo = new ShopInfo(1, "哔哩哔哩", 125.5, "www.blibli.com");
	String json = JSON.toJSONString(shopInfo);
	System.out.println(json);
  }
}

  

  4.将Java对象的List转换为json的字符串[]
    API:
      String toJSONString(Object object);
    步骤:
      1.导入fastjson的jar包
      2.JSON调用toJSONString()方法,获取转换后的json数据
      例如:
        List<ShopInfo> shops = new ArrayList<>();
        ShopInfo shopInfo1 = new ShopInfo(1, "哔哩哔哩", 255.5, "www.blibli.com");
        ShopInfo shopInfo2 = new ShopInfo(1, "豆瓣", 255.5, "www.douban.com");
        ShopInfo shopInfo3 = new ShopInfo(1, "知乎", 255.5, "www.zhihu.com");
        shops.add(shopInfo1);
        shops.add(shopInfo2);
        shops.add(shopInfo3);
        String json = JSON.toJSONString(shops);
    代码:

public class Four {
  public static void main(String[] args) {
	ShopInfo shopInfo1 = new ShopInfo(1, "哔哩哔哩", 255.5, "www.blibli.com");
	ShopInfo shopInfo2 = new ShopInfo(1, "豆瓣", 255.5, "www.douban.com");
	ShopInfo shopInfo3 = new ShopInfo(1, "知乎", 255.5, "www.zhihu.com");
	List<ShopInfo> list = new ArrayList<ShopInfo>();
	list.add(shopInfo1);
	list.add(shopInfo2);
	list.add(shopInfo3);
	String json = JSON.toJSONString(list);
	System.out.println(json);
  }
}

  

最后补一下fastjson在maven中的地址:

  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.32</version>
  </dependency>

最后,再说一句,和本节内容无关的事:

翟哥,我错了,我真的错了,原谅我吧!

 

JSON初体验(三):FastJson解析

标签:体验   api   步骤   ast   pen   ret   div   add   解析json数据   

原文地址:http://www.cnblogs.com/wnbahmbb/p/7019995.html

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