Android GSON使用

Android GSON使用

使用场景及作用:Gson 是用来解析和生成Json,在实际项目中广泛使用。注意定义定对像的属性值必须和json中的保持一致,不然会解析失败。当然这就要求后台命名规范。

集成

dependencies {
  implementation 'com.google.code.gson:gson:2.8.6'
}

一、对象转Json

new Gson().toJson(obj)

二、Json转对象

new Gson().fromJson(String json, Class classOfT)

三、测试结果

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by t, Date on 2019-10-23.
 * note: Gson使用
 */
public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

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

        Gson gson = new Gson();

        Person person = new Person();
        person.setName("张三");
        person.setAge(22);

        Log.d(TAG, "toJson:" + gson.toJson(person));

        String jsonStr = "{\"age\":22,\"name\":\"张三\"}";

        Log.d(TAG, "fromJson:" + new Gson().fromJson(jsonStr, Person.class).toString());

        //来个复杂点的

        CompanyInfo companyInfo = new CompanyInfo();
        companyInfo.setCompanyName("公司名字");
        companyInfo.setCompanyOwner("老板");

        List personList = new ArrayList<>();
        personList.add(new Person("张三", 22));
        personList.add(new Person("李四", 23));
        personList.add(new Person("王五", 24));
        companyInfo.setPersonList(personList);

        String toJson = gson.toJson(companyInfo);

        Log.d(TAG, "toJson:" + toJson);

        Log.d(TAG, "fromJson:" + gson.fromJson(toJson, CompanyInfo.class));
    }


    public class Person {
        private String name;
        private int age;

        public Person() {
        }

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

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

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }


    public class CompanyInfo {
        private String companyName;
        private String companyOwner;
        private List personList;

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public String getCompanyOwner() {
            return companyOwner;
        }

        public void setCompanyOwner(String companyOwner) {
            this.companyOwner = companyOwner;
        }

        public List getPersonList() {
            return personList;
        }

        public void setPersonList(List personList) {
            this.personList = personList;
        }

        @Override
        public String toString() {
            return "CompanyInfo{" +
                    "companyName='" + companyName + '\'' +
                    ", companyOwner='" + companyOwner + '\'' +
                    ", personList=" + personList +
                    '}';
        }
    }

}

Gson处理空对象场景


//        UserInfo userInfo = new UserInfo();
//        userInfo.setId(0);
//        userInfo.setName("张三");
//        userInfo.setAddress(null);

//        {"id":0,"name":"张三"}


//        UserInfo userInfo = new UserInfo();
//        userInfo.setId(0);
//        userInfo.setName("张三");
//
//        {"id":0,"name":"张三"}

//        UserInfo userInfo = new UserInfo();
//        userInfo.setId(0);
//        userInfo.setName("张三");
//        userInfo.setAddress("");
//        
//        {"address":"","id":0,"name":"张三"}



        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("张三");
        userInfo.setAddress("地址");
        userInfo.setObject(new StudentInfo(2));

//        {"address":"地址","data":{"studentId":2},"id":1,"name":"张三"}


你可能感兴趣的:(Android框架与架构)