【问题标题】:How to get json object to array to object under the object get object from array如何获取json对象到数组到对象下的对象从数组中获取对象
【发布时间】:2020-04-23 02:12:36
【问题描述】:

我想解析包括剧集、流派数组在内的完整数据,但我正在使用 volley lib 并从“动漫”数组中获取主要对象!

我也是如何获得 Episodes 数组的数据的? 有什么简单的方法吗?

我还将这些数据添加到单个模型数据中,所以请给我理解代码。

这是我从网上获取的json数据:

{
  "anime": [
    {
      "title": "Master",
      "img": "img.png",
      "synopsis": "Plot Summary:plot",
      "genres": [
        "adventure",
        "drama",
        "historical",
        "mystery",
        "seinen",
        "slice-of-life"
      ],
      "released": 1998,
      "status": "Completed",
      "otherName": "Master",
      "totalEpisodes": 39,
      "episodes": [
        {
          "id": "episode-1"
        },
        {
          "id": "episode-2"
        },
        {
          "id": "episode-3"
        },
        {
          "id": "episode-4"
        },
        {
          "id": "episode-5"
        },
        {
          "id": "episode-39"
        }
      ]
    },
    {
      "title": "Hyper Police",
      "img": "police.png",
      "synopsis": "Plot Summary: plot",
      "genres": [
        "action",
        "comedy",
        "police",
        "romance",
        "sci-fi"
      ],
      "released": 1997,
      "status": "Completed",
      "otherName": " ploci",
      "totalEpisodes": 25,
      "episodes": [
        {
          "id": "episode-1"
        },
        {
          "id": "episode-2"
        },
        {
          "id": "episode-3"
        },
        {
          "id": "episode-25"
        }
      ]
    }
  ]
}

我从 MainActivity 调用此代码但不起作用。


String url = "API URL";
    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response)
        {
            try
            {
            JSONArray jsonArray = response.getJSONArray("anime");

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

                JSONArray data = hit.getJSONArray("episodes");

                // Extract JSONObject inside JSONObject
                JSONObject IDOb = data.getJSONObject(i);
                String ids = IDOb.getString("id");

                String title = hit.getString("title");
                String img = hit.getString("img");
                String synopsis = hit.getString("synopsis");
                        String genre = hit.getString("genres");
                        String release = hit.getString("released");
                        String status = hit.getString("status");
                        String otherName = hit.getString("otherName");
                        String totalEpisodes = hit.getString("totalEpisodes");
                mExampleList.add(new ExampleItem(title, img, synopsis, genre, release, status, otherName, totalEpisodes
                                 , ids));
                loadingPop.dismiss();
                Toast.makeText(getActivity(), "Data Refreshed", Toast.LENGTH_SHORT).show();
            }
            mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
            mRecyclerView.setAdapter(mExampleAdapter);
            Toast.makeText(getActivity(), "Added Data", Toast.LENGTH_SHORT).show();
            //loadingPop.dismiss();
            }
            catch (JSONException e)
            {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Catch" + e, Toast.LENGTH_LONG).show();
            loadingPop.dismiss();

            }}
        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            }});

【问题讨论】:

  • 这能回答你的问题吗? How to parse JSON in Java
  • jsonschema2pojo.org 使用此链接为您的 json 创建模型类。粘贴json,选择目标语言为java,源类型为json,注释样式为Gson,点击“预览”。它将为您创建所有必要的类
  • 我用的是getter模型!和 MainActivity 的 json volley 所以我想在没有 Pojo 方法的情况下在 MainActivity 解析这些数据
  • 显示您的代码和您尝试过的内容。
  • 我编辑了问题并添加了 json 调用的 MainActivity。请再次检查并解决这个问题????

标签: java android json api


【解决方案1】:

我认为问题在于获取完整的 episodes 数据

我会尝试重写你的 for 循环:

.....
.....

try
{


JSONArray animeArray = response.getJSONArray("anime");

//loop through anime array
for (int i = 0; i < animeArray.length(); i++){

//this should run for every object in anime
JSONObject animeObject = animeArray.getJSONObject(i);

//title
String title = animeObject.getString("title");

//img
String img = animeObject.getString("img");

//synopsis
String synopsis = animeObject.getString("synopsis");

//genres
JSONArray genresArray = animeObject.getJSONArray("genres");
String[] genres = new String[genresArray.length()];
for(int i = 0; i < genresArray.length(); i++){
genres[i] = genresArray.getString(i);
}

//released
int released = animeObject.getInt("released");

//status
String status = animeObject.getString("status");

//otherName
String otherName = animeObject.getString("otherName");

//totalEpisodes
int totalEpisodes = animeObject.getInt("totalEpisodes");


//episodes ids
JSONArray episodesArray = animeObject.getJSONArray("episodes");
String[] ids = new String[episodesArray.length()];
for(int i = 0; i < episodesArray.length(); i++){
ids[i] = episodesArray.getJSONObject(i).getString("id");
}





}//end loop


 mExampleList.add(new ExampleItem(title, img, synopsis, genres, released, status, 
 otherName, totalEpisodes,ids));



........
.........
........

现在您更改 ExampleItem 构造函数

public ExampleItem(String title,String img,String synopsis,String[] genres,int released,String status,String otherName,int totalEpisodes,String[] ids){

  ..................
  .................

 }

【讨论】:

  • 从 volley 中获取错误:- org.json.JSONException: org.json.JSONObject$1 类型的 totalEpisodes 处的值为 null 无法转换为 int
  • ?我应该知道些什么?
  • 你的“totalEpisodes”和int还是long
  • 检查我的 json 代码我是从 API 获取的,我不知道它是什么类型的调用。
  • 尝试删除出错的行,看看是否正在解析其他数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 2015-07-29
  • 2019-11-12
  • 2018-06-28
  • 1970-01-01
相关资源
最近更新 更多