Android FastJson使用的四种方式

Fast Json是阿里创建的一款api接口,用于对Json的处理,Fast Json的性能是非常的良好的,解析的速度要超过其他的接口然而他的有点远远不止这些,我们来列举一下他的相关优点吧.

1.首先就是速度上.Fast Json的解析速度是非常高效的,速度快的原因就是使用了优化算法,因此他的速度要远远好于其他的api接口.

2.没有依赖性,在JDK 5.0开始被正式的启用,支持Android,支持的数据类型也是非常的多.

多的废话我也就不多说了,贴上代码(需要导入一个FastJson jar包)。

一、将json对象转换为java对象

Android FastJson使用的四种方式_第1张图片

 private void json() {
        //获取或者创建数组
        String json="{\n" +
                "    \"id\": 2,\n" +
                "    \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
                "    \"name\": \"猴哥\",\n" +
                "    \"price\": 12.3\n" +
                "}";
        //解析json
        shopInfo shopInfo = JSON.parseObject(json, shopInfo.class);
        //显示数据
        mtest01.setText(json);
        mtest02.setText(shopInfo.toString());
    }

还需创建一个实体类 shopInfo .java

package com.example.fastjson;

/**
 * Created by Myzg2 on 2017/7/30.
 */

public class shopInfo {

    /**
     * id : 2
     * imgPath : http://img00.hc360.com/cloth/201206/201206191116426249.jpg
     * name : 猴哥
     * price : 12.3
     */

    private int id;
    private String imgPath;
    private String name;
    private double price;

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

    public shopInfo() {
    }

    @Override
    public String toString() {
        return "shopInfo{" +
                "id=" + id +
                ", imgPath='" + imgPath + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }

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

}

二、将json数组转换为java对象的集合

Android FastJson使用的四种方式_第2张图片

 private void jsonList() {
        //获取或者创建数组
        String json="[\n" +
                "    {\n" +
                "        \"id\": 1,\n" +
                "        \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
                "        \"name\": \"猴哥\",\n" +
                "        \"price\": 12.3\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": 2,\n" +
                "        \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
                "        \"name\": \"八戒\",\n" +
                "        \"price\": 63.2\n" +
                "    }\n" +
                "]";
        //解析json
        List shopInfos = JSON.parseArray(json, shopInfo.class);
        //显示数据
        mtest01.setText(json);
        mtest02.setText(shopInfos.toString());
    }

三、将java对象转化为Json对象

Android FastJson使用的四种方式_第3张图片

  private void javaJson() {
              //获取或者创建java对象
        shopInfo shopInfo = new shopInfo(1, "caomei", "草莓", 36.5);
        //生成json数据
        String s = JSON.toJSONString(shopInfo);
        //显示

        mtest01.setText(shopInfo.toString());
        mtest02.setText(s);
    }

四、将java集合转化为Json数据

Android FastJson使用的四种方式_第4张图片

 private void javaJsonList() {
        //获取或者创建java对象
        List<shopInfo> list =new ArrayList<>();
        shopInfo shopInfo = new shopInfo(1, "caomei", "草莓", 36.5);
        shopInfo shopInfos = new shopInfo(2, "lanmei", "蓝莓", 66.5);
        list.add(shopInfo);
        list.add(shopInfos);

        //生成json数据
        String s = JSON.toJSONString(list);
        //显示

        mtest01.setText(shopInfo.toString());
        mtest02.setText(s);
    }

你可能感兴趣的:(Android)