Gson和Fastjson解析对比

1。PersonBean

package com.louis.fastjsondemo;

/**
 * Created by Administrator on 2016/2/26.
 */
public class PersonBean {

    private int id;
    private String name;
    private String address;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public PersonBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public PersonBean(int id, String name, String address) {
        super();
        this.id = id;
        this.name = name;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "PersonBean [id=" + id + ", name=" + name + ", address=" + address + "]";
    }

}

2。Gson aty

package com.example.louisgsondemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gson gson = new Gson();
        // JavaBean数据
        PersonBean personBean = new PersonBean(1, "name1", "address1");
        String jsonString = gson.toJson(personBean);
        System.out.println("==1.jsonString=="+jsonString);
        // JavaBean数据解析
        PersonBean person = gson.fromJson(jsonString, PersonBean.class);
        System.out.println("==1.person=="+person);
        // List数据
        PersonBean person1 = new PersonBean(1001, "香港", "刘德华");

        PersonBean person2 = new PersonBean(1002, "韩国", "宋慧乔");

        List<PersonBean> persons = new ArrayList<PersonBean>();

        persons.add(person1);

        persons.add(person2);
        jsonString = gson.toJson(persons);
        System.out.println("==2.jsonString=="+jsonString);
        // List数据解析
        List<PersonBean> persons2 = gson.fromJson(jsonString, new TypeToken<List<PersonBean>>() {
        }.getType());
        System.out.println("==2.persons2=="+persons2);

        // List数据
        List<String> list = new ArrayList<String>();

        list.add("gson1");

        list.add("gson2");

        list.add("gson3");
        jsonString = gson.toJson(list);
        System.out.println("==3.jsonString=="+jsonString);
        // List数据解析
        List<String> list2 = gson.fromJson(jsonString, new TypeToken<List<String>>() {
        }.getType());
        System.out.println("==3.list2=="+list2);

        // List>数据
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("key1", "value1");

        map.put("key2", "value2");

        Map<String, Object> map2 = new HashMap<String, Object>();

        map2.put("key1", 1);

        map2.put("key2", 2);

        List<Map<String, Object>> listm = new ArrayList<Map<String, Object>>();

        listm.add(map);

        listm.add(map2);
        jsonString = gson.toJson(listm);
        System.out.println("==4.jsonString=="+jsonString);
        List<Map<String, Object>> listm2 = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() {
        }.getType());
        System.out.println("==4.listm2=="+listm2);
    }
}

3。Fastjson aty

package com.louis.fastjsondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // JavaBean数据
        PersonBean personBean = new PersonBean(1, "name1", "address1");
        String jsonString = JSON.toJSONString(personBean);
        System.out.println("louis==1.jsonString==" + jsonString);

        // JavaBean数据解析
        PersonBean person =JSON.parseObject(jsonString, PersonBean.class);
        System.out.println("louis==1.person=="+person);

        // List数据
        PersonBean person1 = new PersonBean(1001, "香港", "刘德华");
        PersonBean person2 = new PersonBean(1002, "韩国", "宋慧乔");
        List persons = new ArrayList();
        persons.add(person1);
        persons.add(person2);
        jsonString = JSON.toJSONString(persons);
        System.out.println("louis==2.jsonString=="+jsonString);

        // List数据
        List list = new ArrayList();
        list.add("fasjson1");
        list.add("fasjson2");
        list.add("fasjson3");
        jsonString =JSON.toJSONString(list);
        System.out.println("louis==3.jsonString=="+jsonString);

        // List数据解析
        List list2 = JSON.parseArray(jsonString, String.class);
        System.out.println("louis==3.list2=="+list2);

        // List>数据
        Map map = new HashMap();

        map.put("key1", "value1");
        map.put("key2", "value2");

        Map map2 = new HashMap();

        map2.put("key1", 1);
        map2.put("key2", 2);

        List> listm = new ArrayList>();

        listm.add(map);
        listm.add(map2);

        jsonString = JSON.toJSONString(listm);
        System.out.println("louis==4.jsonString=="+jsonString);

        List> listm2 = JSON.parseObject(jsonString, new TypeReference>>(){});
        System.out.println("louis==4.listm2=="+listm2);
    }
}

你可能感兴趣的:(Android,综合知识)