【问题标题】:Android Multiple Level JsonObject and JsonArray Parsing with Model classAndroid 多级 JsonObject 和 JsonArray 解析与模型类
【发布时间】:2019-03-20 16:35:54
【问题描述】:
{
"Query":"query",
"KMAQuery":"query",
"TotalCount":3,
"Data":[{"CollName":"kmdb_new",
         "TotalCount":3,
         "Count":3,
         "Result":[{"title":"sampletitle",
                    "director":[{"directorNm":"name1","directorId":"00004544"}],
                    "nation":"nation1",
                    "company":"company1",
                    "genre":"genre1",
                    "kmdbUrl":"http://www.kmdb.or.kr/vod/vod_basic.asp?nation=K&p_dataid=01040",
                    "rating":[{"ratingMain":"Y","ratingDate":"19640717","ratingNo":"","ratingGrade":"","releaseDate":"","runtime":""}]]}

这是我从 OKHttp 解析得到的 Json 数据。

其实有很多相同的Result 2~3。

我要解析键名“title”、“directorNm”、“nation”、“company”、“ratingGrade”并设置Model类。

如何将多个带有Gson的Json Object和Array解析成Model类?

我终于要在模型类中使用recyclerview了。

如果你告诉我如何解析“title”和“directorNm”,我可以休息一下。

作为参考,我正在使用 AsyncTask、OKHttp、Gson 等。

如果你不明白我的问题或需要代码,请评论!

我非常需要你的帮助。

【问题讨论】:

  • 另外,我使用的是 AsyncTask。如何在 doInBackground 中解析并返回 onPostExecute?
  • @NileshRathod 我现在就试试!
  • @NileshRathod 我的 pojo 类是对的,但我不知道如何在 Android Activity 中将值设置为模型
  • 你可以使用Gson github.com/google/gson

标签: android json parsing gson


【解决方案1】:

这里分享的是Model(Pojo Class)的代码设置响应

public void getResponse(JSONObject jsonObject){
    try {
        JSONObject responseJson = new JSONObject(jsonObject.toString());

        JSONArray jsonArray = responseJson.getJSONArray("heroes");

        for (int i = 0; i < jsonArray.length(); i++){

                //getting the json object of the particular index inside the array
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                YourPojoClass pojoObject = new YourPojoClass();

                pojoObject.setName(jsonObject.getString("name"));
                pojoObject.setImageurl(jsonObject.getString("imageurl"));

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 你可以把这个函数放在你的 asynchTask 的 onPostExecute 方法中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多