Fastjson和Gson解析

Fastjson和Gson解析Json

   private String feedStr = "{\"feedStr\":\"aing\",\n" +
            " \"feedInt\":110,\n" +
            " \"feedFloat\":1300.14,\n" +
            " \"feedBoo\":true,\n" +
            " \"feedList\":[\"a1\",\"b1\",\"c1\"],\n" +
            " \"feedMap\":{\"k1\":\"v1\",\"k2\":\"v2\"},\n" +
            " \"bar\":{\"bfeed1\":\"bv1\",\"bfeed2\":\"bv2\"}\n" +
            "}";

    private String feedList = "[\"a1\",\"b1\",\"c1\"]";

    String parsingStr = "[{\"feedId\":\"1\",\"feedName\":\"feedGeenk\"},{\"feedId\":\"2\",\"feedName\":\"alibaba\"}]";

build.gradle添加

implementation "com.alibaba:fastjson:1.1.70.android"
implementation 'com.google.code.gson:gson:2.8.5'

FeedBean

public class FeedBean implements Parcelable {

    private String feedId;
    private String feedName;
    private String feedStr;
    private int feedInt;
    private float feedFloat;
    private boolean feedBoo;
    private List feedList;
    private FeedChildBean feedMap;
    private FeedChildBean bar;

    public String getFeedId() {
        return feedId;
    }

    public void setFeedId(String feedId) {
        this.feedId = feedId;
    }

    public String getFeedName() {
        return feedName;
    }

    public void setFeedName(String feedName) {
        this.feedName = feedName;
    }

    public String getFeedStr() {
        return feedStr;
    }

    public void setFeedStr(String feedStr) {
        this.feedStr = feedStr;
    }

    public int getFeedInt() {
        return feedInt;
    }

    public void setFeedInt(int feedInt) {
        this.feedInt = feedInt;
    }

    public float getFeedFloat() {
        return feedFloat;
    }

    public void setFeedFloat(float feedFloat) {
        this.feedFloat = feedFloat;
    }

    public boolean isFeedBoo() {
        return feedBoo;
    }

    public void setFeedBoo(boolean feedBoo) {
        this.feedBoo = feedBoo;
    }

    public List getFeedList() {
        return feedList;
    }

    public void setFeedList(List feedList) {
        this.feedList = feedList;
    }

    public FeedChildBean getFeedMap() {
        return feedMap;
    }

    public void setFeedMap(FeedChildBean feedMap) {
        this.feedMap = feedMap;
    }

    public FeedChildBean getBar() {
        return bar;
    }

    public void setBar(FeedChildBean bar) {
        this.bar = bar;
    }

    class FeedChildBean implements Parcelable {
        private String k1;
        private String k2;
        private String bfeed1;
        private String bfeed2;

        public String getK1() {
            return k1;
        }

        public void setK1(String k1) {
            this.k1 = k1;
        }

        public String getK2() {
            return k2;
        }

        public void setK2(String k2) {
            this.k2 = k2;
        }

        public String getBfeed1() {
            return bfeed1;
        }

        public void setBfeed1(String bfeed1) {
            this.bfeed1 = bfeed1;
        }

        public String getBfeed2() {
            return bfeed2;
        }

        public void setBfeed2(String bfeed2) {
            this.bfeed2 = bfeed2;
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.k1);
            dest.writeString(this.k2);
            dest.writeString(this.bfeed1);
            dest.writeString(this.bfeed2);
        }

        public FeedChildBean() {
        }

        protected FeedChildBean(Parcel in) {
            this.k1 = in.readString();
            this.k2 = in.readString();
            this.bfeed1 = in.readString();
            this.bfeed2 = in.readString();
        }

        public  final Creator CREATOR = new Creator() {
            @Override
            public FeedChildBean createFromParcel(Parcel source) {
                return new FeedChildBean(source);
            }

            @Override
            public FeedChildBean[] newArray(int size) {
                return new FeedChildBean[size];
            }
        };
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.feedId);
        dest.writeString(this.feedName);
        dest.writeString(this.feedStr);
        dest.writeInt(this.feedInt);
        dest.writeFloat(this.feedFloat);
        dest.writeByte(this.feedBoo ? (byte) 1 : (byte) 0);
        dest.writeStringList(this.feedList);
    }

    public FeedBean() {
    }

    protected FeedBean(Parcel in) {
        this.feedId = in.readString();
        this.feedName = in.readString();
        this.feedStr = in.readString();
        this.feedInt = in.readInt();
        this.feedFloat = in.readFloat();
        this.feedBoo = in.readByte() != 0;
        this.feedList = in.createStringArrayList();
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public FeedBean createFromParcel(Parcel source) {
            return new FeedBean(source);
        }

        @Override
        public FeedBean[] newArray(int size) {
            return new FeedBean[size];
        }
    };
}

  1. Fastjson解析
//fastjson解析
    private void fastjsonParsing() {
        FeedBean mFeedBean = JSON.parseObject(feedStr, FeedBean.class);
        Log.e("fastjson解析feedStr", "fastjson解析feedStr getFeedStr " + mFeedBean.getFeedStr());
        Log.e("fastjson解析feedStr", "fastjson解析feedStr feedFloat " + mFeedBean.getFeedFloat());
        Log.e("fastjson解析feedStr", "fastjson解析feedStr feedMap k1 " + mFeedBean.getFeedMap().getK1());
        Log.e("fastjson解析feedStr", "fastjson解析feedStr bar bfeed1 " + mFeedBean.getBar().getBfeed1());

        JSONArray mJSONArray = JSON.parseArray(feedList);
        for (int i = 0; i < mJSONArray.size(); i++) {
            Log.e("fastjson解析bar", "fastjson解析bar " + mJSONArray.get(i));
        }

        List mFeedList = JSON.parseArray(parsingStr, FeedBean.class);
        for (int i = 0; i < mFeedList.size(); i++) {
            Log.e("fastjson解析parsingStr", "fastjson解析parsingStr  getFeedId " + mFeedList.get(i).getFeedId());
            Log.e("fastjson解析parsingStr", "fastjson解析parsingStr getFeedName " + mFeedList.get(i).getFeedName());
        }
    }

Fastjson和Gson解析_第1张图片

  1. Gson解析
 //gson解析
    private void gsonParsing() {
        FeedBean mFeedBean = new GsonBuilder().create().fromJson(feedStr, FeedBean.class);
        Log.e("gson解析feedStr", "gson解析feedStr getFeedStr " + mFeedBean.getFeedStr());
        Log.e("gson解析feedStr", "gson解析feedStr feedFloat " + mFeedBean.getFeedFloat());
        Log.e("gson解析feedStr", "gson解析feedStr feedMap k1 " + mFeedBean.getFeedMap().getK1());
        Log.e("gson解析feedStr", "gson解析feedStr bar bfeed1 " + mFeedBean.getBar().getBfeed1());

        List mJSONArray = new GsonBuilder().create().fromJson(feedList, new TypeToken>() {
        }.getType());
        for (int i = 0; i < mJSONArray.size(); i++) {
            Log.e("gson解析bar", "gson解析bar " + mJSONArray.get(i));
        }

        List mFeedList = (List) new GsonBuilder().create().fromJson(parsingStr, new TypeToken>() {
        }.getType());
        for (int i = 0; i < mFeedList.size(); i++) {
            Log.e("gson解析parsingStr", "gson解析parsingStr  getFeedId " + mFeedList.get(i).getFeedId());
            Log.e("gson解析parsingStr", "gson解析parsingStr getFeedName " + mFeedList.get(i).getFeedName());
        }
    }

Fastjson和Gson解析_第2张图片

你可能感兴趣的:(Json基础)