Gson简单实例

public class GsonActivity extends Activity {

    private TextView nameView;
    private TextView ageView;
    private TextView timeView;

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

        nameView = (TextView) findViewById(R.id.nameView);
        ageView = (TextView) findViewById(R.id.ageView);
        timeView = (TextView) findViewById(R.id.timeView);

        Student student = new Student("aa",12,new Date());
        Student student1 = new Student("bb",21,new Date());

        List<Student> list = new ArrayList<Student>();
        list.add(student);
        list.add(student1);

        Gson gson = new Gson();
        String jsonList = gson.toJson(list);

        list=gson.fromJson(jsonList,new TypeToken<List<Student>>(){}.getType());
        for(Student s:list){
            nameView.setText(s.getName());
            ageView.setText(s.getAge()+"");
            timeView.setText(s.getBrithday().toString());
        }
    }
}
public class Student {
    private String name;
    private int age;
    private Date brithday;

    public Student(String name, int age, Date brithday) {
        this.name = name;
        this.age = age;
        this.brithday = brithday;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }


    public Date getBrithday() {
        return brithday;
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/nameView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_marginLeft="50dp"
        android:text="姓名" />

    <TextView
        android:id="@+id/ageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView"
        android:layout_marginTop="50dp"
        android:text="年龄" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ageView"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:text="姓名" />

    <TextView
        android:id="@+id/timeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView"
        android:layout_marginTop="100dp"
        android:text="生日" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ageView"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="100dp"
        android:text="姓名" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="110dp" />

    <ImageView
        android:id="@+id/Iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:src="@drawable/abc_ab_share_pack_mtrl_alpha" />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值