0

这是我的json

{
    "message":"Success",
    "method":"getList",
    "values": [
        {
            "_id":"2"
        }
    ]
}

有两个模型类:

public class ModelList {
    @SerializedName("message")
    public String MESSAGE;

    @SerializedName("method")
    public String METHOD;

    public List<ModelValue> modelValues;
}

和:

public class ModelValue {

    @SerializedName("_id")
    public String ARTIST_ID;
}

这是我在活动中的代码。

ModelList response;

response = gson.fromJson(jsonToStringFromAssetFolder("twitter_search.json", JsonParsingActivity.this),
    ModelArtistList.class);
Toast.makeText(this, response.MESSAGE, Toast.LENGTH_SHORT).show();

List<ModelValue> results = response.modelValues;

for (ModelValue result : results) {
    Toast.makeText(this, result._ID, Toast.LENGTH_SHORT).show();
    Log.e("log_tag","result.fromUser"+result._ID);
}

我得到NullPointerException了:List<ModelValue> results = response.modelValues;

4

1 回答 1

1

我认为您在课堂上需要这个ModelList:添加@SerializedName("values")modelValues

public class ModelList {

    @SerializedName("message")
    public String MESSAGE;
    @SerializedName("method")
    public String METHOD;
    @SerializedName("values")
    public List<ModelValue> modelValues;
}
于 2013-10-23T07:08:17.463 回答