Android JSON数据与实体类之间的相互转化


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class JsonTestActivity extends Activity implements OnClickListener {
    private Button btnShitiToJson;
    private Button btnJsonToShiti;
    private Button btnJsonToList;
    private Button btnListToJson;

    private Gson gson;
    private GsonBuilder builder;

    private Person person;

    private String jsonTest;
    private String jsonListTest;
    private List persons;

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

    private void init() {
        btnShitiToJson=(Button) findViewById(R.id.btn_shiti_to_json);
        btnJsonToShiti=(Button) findViewById(R.id.btn_json_to_shiti);
        btnListToJson=(Button) findViewById(R.id.btn_list_to_json);
        btnJsonToList=(Button) findViewById(R.id.btn_json_to_list);
        btnShitiToJson.setOnClickListener(this);
        btnJsonToShiti.setOnClickListener(this);
        btnListToJson.setOnClickListener(this);
        btnJsonToList.setOnClickListener(this);

        builder=new GsonBuilder();
        gson=builder.create();
        initData();
    }

    private void initData() {
        person=new Person();
        person.setName("张三");
        person.setAge(20);
        person.setTall(160);

        persons=new ArrayList();
        for(int i=0;i<3;i++){
            Person p1=new Person();
            p1.setName("李四"+i);
            p1.setAge(23+i);
            p1.setTall(165+i);
            persons.add(p1);
        }
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_shiti_to_json:
            //实体类转换为json数据
            jsonTest=gson.toJson(person, Person.class);
            Log.e("test", jsonTest);
            //打印结果为:{"name":"张三","age":20,"tall":160}
            break;
        case R.id.btn_json_to_shiti:
            //json数据转换为实体类
            Person p=gson.fromJson(jsonTest, Person.class);
            Log.e("test", p.getName()+" "+p.getAge()+" "+p.getTall());
            //打印结果为:张三 20 160
            break;
        case R.id.btn_list_to_json:
            //存储实体类的集合转换为json数据集合
            Type type=new TypeToken>(){}.getType();
            jsonListTest=gson.toJson(persons, type);
            Log.e("test", jsonListTest);
            //打印结果为:[{"name":"李四0","age":23,"tall":165},{"name":"李四1","age":24,"tall":166},{"name":"李四2","age":25,"tall":167}]
            break;
        case R.id.btn_json_to_list:
            //json数据的集合转换为存储实体类的集合
            List p2=new ArrayList();
            Type type1=new TypeToken>(){}.getType();
            p2=gson.fromJson(jsonListTest, type1);
            Log.e("test", String.valueOf(p2.size()));
            //打印结果为:3
            break;
        }
    }
    
    private class Person {
        private String name;
        private int age;
        private int tall;
        public String getName() {
            return this.name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return this.age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getTall() {
            return this.tall;
        }
        public void setTall(int tall) {
            this.tall = tall;
        }
    }
}

 

你可能感兴趣的:(Android相关)