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<JavaBean>数据
        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<JavaBean>数据解析
        List<PersonBean> persons2 = gson.fromJson(jsonString,new TypeToken<List<PersonBean>>() {
        }.getType());
        System.out.println("==2.persons2=="+persons2);

        // List<String>数据
        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<String>数据解析
        List<String> list2 = gson.fromJson(jsonString,new TypeToken<List<String>>() {
        }.getType());
        System.out.println("==3.list2=="+list2);

        // List<Map<String,Object>>数据
        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@H_403_578@.louis@H_403_578@.fastjsondemo;

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

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

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

public class MainActivity extends AppCompatActivity {

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

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

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

        // List<JavaBean>数据
        PersonBean person1 = new PersonBean(1001,"刘德华");
        PersonBean person2 = new PersonBean(1002,"宋慧乔");
        List<PersonBean> persons = new ArrayList<PersonBean>();
        persons@H_403_578@.add(person1);
        persons@H_403_578@.add(person2);
        jsonString = JSON@H_403_578@.toJSONString(persons);
        System@H_403_578@.out@H_403_578@.println("louis==2.jsonString=="+jsonString);

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

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

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

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

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

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

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

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

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

        List<Map<String,Object>> listm2 = JSON@H_403_578@.parSEObject(jsonString,new TypeReference<List<Map<String,Object>>>(){});
        System@H_403_578@.out@H_403_578@.println("louis==4.listm2=="+listm2);
    }
}

相关文章

  jsonp需要在页面中添加一个<script>元素,由该元素来从其他服务器加载json数据。 <body&g...
<script> var testApi = "地址"; $.ajax({ url:testApi,//可以不是本地域名 type:‘post...
总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : Syst...
最近开发中遇到调用第三方web_api的功能,后端在处理json数据时使用fastjson来做反序列化,由于调用api...
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp...
JsonSerializer有多个属性,用于自定义如何序列化JSON。这些也可以通过JsonSerializerSettings参数,在...