无法通过gson解析JsonArray

时间:2013-10-23 07:05:09

标签: android json gson

这是我的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;
}

这是我在Activity中的代码。

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;

1 个答案:

答案 0 :(得分:1)

我认为您需要ModelList课程:@SerializedName("values")

添加modelValues
public class ModelList {

    @SerializedName("message")
    public String MESSAGE;
    @SerializedName("method")
    public String METHOD;
    @SerializedName("values")
    public List<ModelValue> modelValues;
}