如何使用Android中的Gson库解析json响应?

时间:2018-12-19 05:44:53

标签: android json gson

我对api的响应如下:

{
"0": "_serialize",
"1": [
    "login"
],
"users": {
    "token": "aaaaa",
    "message": "login successful."
    }
  }

如何使用Gson在android中对此进行解析?

4 个答案:

答案 0 :(得分:1)

首先使用this one之类的工具在您的 json 中创建 model 类。只需复制json并获取对象即可。

假设您将类称为Model

然后使用此code从json中获取对象

String json = "that-json";
Model m = gson.fromJson(json, Model.class);

有关Gson's guides的更多信息。

答案 1 :(得分:1)

从响应中生成Model类

<div class="container">
    <div class="item item-1">
    	1
    </div>
    <div class="item item-2">
    	2
    </div>
    <div class="item item-3">
    	3
    </div>
    <div class="item item-4">
    	4
    </div>
    <div class="item item-5">
    	5
    </div>
    <div class="item item-6">
    	6
    </div>
    <div class="item item-7">
    	7
    </div>
    <div class="item item-8">
    	8
    </div>
    <div class="item item-9">
    	9
    </div>
    <div class="item item-10">
    	10
    </div>
    <div class="item item-11">
    	11
    </div>
    <div class="item item-12">
    	12
    </div>
  </div>

答案 2 :(得分:1)

使用gson并将您的响应映​​射到Java Model类。

这将是您的模型课->

User.java模型类。

import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Users implements Serializable
{

    @SerializedName("token")
    @Expose
    private String token;
    @SerializedName("message")
    @Expose
    private String message;
    private final static long serialVersionUID = 3387741697269012981L;

    /**
     * No args constructor for use in serialization
     * 
     */
    public Users() {
    }

    /**
     * 
     * @param message
     * @param token
     */
    public Users(String token, String message) {
        super();
        this.token = token;
        this.message = message;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Users withToken(String token) {
        this.token = token;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Users withMessage(String message) {
        this.message = message;
        return this;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("token", token).append("message", message).toString();
    }

}

这是您的根/父类,从那里开始解析。

import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Root implements Serializable
{

    @SerializedName("0")
    @Expose
    private String _0;
    @SerializedName("1")
    @Expose
    private List<String> _1 = null;
    @SerializedName("users")
    @Expose
    private Users users;
    private final static long serialVersionUID = 5880229946098431789L;

    /**
     * No args constructor for use in serialization
     * 
     */
    public Example() {
    }

    /**
     * 
     * @param users
     * @param _0
     * @param _1
     */
    public Example(String _0, List<String> _1, Users users) {
        super();
        this._0 = _0;
        this._1 = _1;
        this.users = users;
    }

    public String get0() {
        return _0;
    }

    public void set0(String _0) {
        this._0 = _0;
    }

    public Example with0(String _0) {
        this._0 = _0;
        return this;
    }

    public List<String> get1() {
        return _1;
    }

    public void set1(List<String> _1) {
        this._1 = _1;
    }

    public Example with1(List<String> _1) {
        this._1 = _1;
        return this;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public Example withUsers(Users users) {
        this.users = users;
        return this;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("_0", _0).append("_1", _1).append("users", users).toString();
    }

}

现在在代码中,您将从此处开始解析的根/父类接收Json map json。

Root root = new Gson().fromJson(/*put your json response variable here*/, Root.class);

并使用根对象通过公共get方法获取/设置任何数据。

答案 3 :(得分:1)

为您的Json创建一个pojo。您可以使用许多在线资源来做到这一点,例如thisthis

现在使用以下格式。

Gson gson = new Gson();
MyPojo myPojo = new MyPojo();
Type collectionType = new TypeToken<MyPojo>() {
                    }.getType();
myPojo = gson.fromJson(responseJson,
                            collectionType);

希望这会有所帮助。

相关问题