如何使用JSON:正确的方法

时间:2016-07-16 07:05:38

标签: android json gson android-json

在Android开发中处理JSON的最佳方法是什么?

应该有一种优雅的方式,我确信这个问题困扰了很多初学者。

1 个答案:

答案 0 :(得分:1)

工具:

  1. 请为您的类使用POJO,这些类旨在被序列化:http://www.jsonschema2pojo.org
  2. 使用GSON(杰克逊)进行JSON序列化/反序列化https://github.com/google/gson
  3. 这里有多么神奇:

    创建班级的JSON表示:

    {
       "name": "Jack",
       "tel": "+79998764521",
       "address": "Liberty St, 8 apt. 87"
    }
    

    使用POJO生成器为您创建一个类。最好确保:

    • 来源类型:JSON
    • 注释风格:GSON

    Employee.java

    package com.bidwingames.app.bidrush.model;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    import org.apache.commons.lang.builder.ToStringBuilder;
    
    import javax.annotation.Generated;
    
    @Generated("org.jsonschema2pojo")
    public class Employee {
    
        @SerializedName("name")
        @Expose
        private String name;
        @SerializedName("tel")
        @Expose
        private String tel;
        @SerializedName("address")
        @Expose
        private String address;
    
        /**
         * No args constructor for use in serialization
         *
         */
        public Employee() {
        }
    
        /**
         *
         * @param address
         * @param tel
         * @param name
         */
        public Employee(String name, String tel, String address) {
            this.name = name;
            this.tel = tel;
            this.address = address;
        }
    
        /**
         *
         * @return
         * The name
         */
        public String getName() {
            return name;
        }
    
        /**
         *
         * @param name
         * The name
         */
        public void setName(String name) {
            this.name = name;
        }
    
        public Employee withName(String name) {
            this.name = name;
            return this;
        }
    
        /**
         *
         * @return
         * The tel
         */
        public String getTel() {
            return tel;
        }
    
        /**
         *
         * @param tel
         * The tel
         */
        public void setTel(String tel) {
            this.tel = tel;
        }
    
        public Employee withTel(String tel) {
            this.tel = tel;
            return this;
        }
    
        /**
         *
         * @return
         * The address
         */
        public String getAddress() {
            return address;
        }
    
        /**
         *
         * @param address
         * The address
         */
        public void setAddress(String address) {
            this.address = address;
        }
    
        public Employee withAddress(String address) {
            this.address = address;
            return this;
        }
    
        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    
        @Override
        public int hashCode() {
            return new HashCodeBuilder().append(name).append(tel).append(address).toHashCode();
        }
    
        @Override
        public boolean equals(Object other) {
            if (other == this) {
                return true;
            }
            if ((other instanceof Employee) == false) {
                return false;
            }
            Employee rhs = ((Employee) other);
            return new EqualsBuilder().append(name, rhs.name).append(tel, rhs.tel).append(address, rhs.address).isEquals();
        }
    
    }
    

    现在,一切都已经完成了:getter和setter,构造函数,构建器和.equals方法(有时候非常重要)

    用法:

    现在,让我们想象一下,我们想要将我们用于构建类的JSON反序列化为对象。很简单:

    String JSON = "{\n" +
                    "\t\"name\": \"Jack\",\n" +
                    "\t\"tel\": \"+79998764521\",\n" +
                    "\t\"address\": \"Liberty St, 8 apt. 87\"\n" +
                    "}";
    
    // Deserialize
    Employee employee = new Gson().fromJson(JSON, Employee.class);
    
    // Serialize
    String serializedJSON = new Gson().toJson(employee);
    

    就是这样!简单而优雅!此外,使用POJO为您提供了使用Retrofit之类的优势,这意味着您的模型为ConverterFactory - 准备就绪:GsonConverterFactory将自动准备呼叫结束的所有数据等。