-2

我刚刚开始学习如何在 Android 中使用 JSON。我应该制作一个显示新闻标题、部分和作者的应用程序。

我不确定问题出在解析中还是其他原因,但它一直告诉我没有数据。

因此,如果它有问题,我可以在这部分获得一些帮助,以便我可以修复它或更多地查看问题所在。

JSON代码:

{
   "response": {
        "status": "ok",
        "userTier": "developer",
        "total": 191,
        "startIndex": 1,
        "pageSize": 10,
        "currentPage": 1,
        "pages": 20,
        "orderBy": "relevance",
        "results": [{
            "id": "film/2017/may/25/12-jours-review-raymond-depardon-documentary-psychiatric-hospital-judge",
            "type": "article",
            "sectionId": "film",
            "sectionName": "Film",
            "webPublicationDate": "2017-05-25T15:37:15Z",
            "webTitle": "12 Jours review – a devastating glimpse into broken souls",
            "webUrl": "https://www.theguardian.com/film/2017/may/25/12-jours-review-raymond-depardon-documentary-psychiatric-hospital-judge",
            "apiUrl": "https://content.guardianapis.com/film/2017/may/25/12-jours-review-raymond-depardon-documentary-psychiatric-hospital-judge",
            "fields": {
                "headline": "12 Jours review – a devastating glimpse into broken souls",
                "starRating": "4",
                "shortUrl": "https://gu.com/p/6g6hn",
                "thumbnail": "https://media.guim.co.uk/1dbf594e183ebe5428fe88c82784c55908b4753c/0_0_3598_2160/500.jpg"
            },
            "tags": [{
                "id": "profile/wendy-ide",
                "type": "contributor",
                "sectionId": "film",
                "sectionName": "Film",
                "webTitle": "Wendy Ide",
                "webUrl": "https://www.theguardian.com/profile/wendy-ide",
                "apiUrl": "https://content.guardianapis.com/profile/wendy-ide",
                "references": [],
                "firstName": "wendy",
                "lastName": "ide"
            }],
            "isHosted": false,
            "pillarId": "pillar/arts",
            "pillarName": "Arts"
        }]
    }
}

我只需要title,sectionauthor. url所以我这样写:

 private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty(newsJSON)) {
        return null;
    }
    List<News> news = new ArrayList<>();
    try {


      JSONObject baseJsonResponse = new JSONObject(newsJSON);
        String response = baseJsonResponse.getString("response");
        JSONObject object = new JSONObject(response);;
        JSONArray newsArray=object.getJSONArray("results");
        for (int i = 0; i < newsArray.length(); i++) {
            JSONObject currentNews = newsArray.getJSONObject(i);
            JSONObject results = currentNews.getJSONObject("results");
            String title = results.getString("webTitle");
            String section = results.getString("sectionName");
            String author = results.getString("firstName");
            String url = results.getString("webUrl");

            News nNews = new News(title, section, author, url);

            news.add(nNews);
        }

    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return news;
}

这对我来说是一个新概念,我很困惑,我真的很感激一些帮助。

编辑:

我在同一个应用程序中使用加载器,如果列表为空,它将显示一个没有数据的字符串,这就是它不断出现的内容。我不知道有什么问题。

public void onLoadFinished(Loader<List<News>> loader, List<News> news) {
    View loadingIndicator = findViewById(R.id.loading_indicator);
    loadingIndicator.setVisibility(View.GONE);

    emptyTextView.setText(R.string.no_news);
    newsAdapter.clear();
    if (news != null && !news.isEmpty()) {
        newsAdapter.addAll(news);
    }
}
4

7 回答 7

0

删除此行

String response = baseJsonResponse.getString("response");

并更改此行

    JSONObject object = new JSONObject(response);

    JSONObject object = baseJsonResponse.getJSONObject("response");
于 2018-01-13T06:00:22.563 回答
0

试试这个代码,

JSONObject baseJsonResponse = new JSONObject(newsJSON);
     JSONObject resJson =baseJsonResponse.getJSONObject("response");
     String status = resJson.getString("status");
JSONArray newsArray=resJson .getJSONArray("results");
于 2018-01-13T06:11:26.727 回答
0

在你的代码中替换这个循环,其他一切似乎都很好。

   private static List<News> extractFeatureFromJson(String newsJSON) {
        if (TextUtils.isEmpty(newsJSON)) {
            return null;
        }
        List<News> news = new ArrayList<>();
        try {
            JSONObject baseJsonResponse = new JSONObject(newsJSON);
            String response = baseJsonResponse.getString("response");
            JSONObject object = new JSONObject(response);;
            String status = object.getString("status");
            if(status.equals("ok")){
            JSONArray newsArray=object.getJSONArray("results");
            for (int i = 0; i < newsArray.length(); i++) {
                JSONObject currentNews = newsArray.getJSONObject(i);
                JSONObject results = currentNews.getJSONObject("results");
                String title = results.getString("webTitle");
                String section = results.getString("sectionName");
                String author = results.getString("firstName");
                String url = results.getString("webUrl");

                News nNews = new News(title, section, author, url);

                news.add(nNews);
            }
            }else{
                //Invalid response Toast here
            }

        } catch (JSONException e) {
            Log.e("QueryUtils", "Problem parsing the news JSON results", e);
        }
        return news;
    }
于 2018-01-13T06:11:35.643 回答
0

您的 Json 类型错误!括号不匹配

于 2018-01-13T06:13:20.350 回答
0

将此代码用于 extractFeatureFromJson

private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty(newsJSON)) {
        return null;
    }
    List<News> news = new ArrayList<>();
    try {
        JSONObject baseJsonResponse = new JSONObject(newsJSON);
        String response = baseJsonResponse.getString("response");
        JSONObject object = new JSONObject(response);;
        JSONArray newsArray=object.getJSONArray("results");
        for (int i = 0; i < newsArray.length(); i++) {
            JSONObject currentNews = newsArray.getJSONObject(i);
            String title = currentNews.getString("webTitle");
            String section = currentNews.getString("sectionName");
            JSONArray tag = currentNews.getJSONArray("tags");
            String firstName;
            if(tag.length() != 0){
                firstName = tag.getJSONObject(0).getString("firstName");
            }
            String url = currentNews.getString("webUrl");

            News nNews = new News(title, section, author, url);

            news.add(nNews);
        }

    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return news;
}
于 2018-01-13T06:19:24.837 回答
0

你不应该使用JSONObject results = currentNews.getJSONObject("results");,因为你已经得到了结果数组,JSONArray newsArray=object.getJSONArray("results"); 试试这个

                for (int i = 0; i < newsArray.length(); i++) {            
                JSONObject results = newsArray.getJSONObject(i);
                String title = results.getString("webTitle");
                String section = results.getString("sectionName");
                String author = results.getString("firstName");
                String url = results.getString("webUrl");

                News nNews = new News(title, section, author, url);

                news.add(nNews);
            }
于 2018-01-13T06:24:37.650 回答
0
        try
        {

            JSONObject jsonObject = new JSONObject("Your_json");

            // get response object
            JSONObject jsonObjectResponse = jsonObject.getJSONObject("response");

            /*
                get Status string from jsonObjectResponse
                you can also get | userTier | total | startIndex and other strings in this json Object
             */
            String status = jsonObjectResponse.getString("status");

            // get result as Json array from jsonObjectResponse
            JSONArray jsonArrayResults = jsonObjectResponse.getJSONArray("results");
            for (int i = 0; i < jsonArrayResults.length() ; i++)
            {
                JSONObject jsonObjectResult = jsonArrayResults.getJSONObject(i);
                String id = jsonObjectResult.getString("id");
                String type = jsonObjectResult.getString("type");
                String sectionId = jsonObjectResult.getString("sectionId");
                String sectionName = jsonObjectResult.getString("sectionName");
                String webPublicationDate = jsonObjectResult.getString("webPublicationDate");
                String webTitle = jsonObjectResult.getString("webTitle");
                String webUrl = jsonObjectResult.getString("webUrl");
                String apiUrl = jsonObjectResult.getString("apiUrl");
                JSONObject jsonObjectfields = new JSONObject(jsonObjectResult.getString("fields"));
                String fieldHeadLine = jsonObjectfields.getString("headline");
                String fieldStarRating = jsonObjectfields.getString("starRating");
                String fieldShortUrl = jsonObjectfields.getString("shortUrl");
                String fieldThumbnail = jsonObjectfields.getString("thumbnail");

                JSONArray jsonArrayTags = jsonObjectResult.getJSONArray("tags");
                for (int j = 0; j < jsonArrayTags.length(); j++)
                {
                    JSONObject jsonObjectTags = jsonArrayTags.getJSONObject(j);
                    String tagID = jsonObjectTags.getString("id");
                    String tagType = jsonObjectTags.getString("type");
                    String tagSelectionID = jsonObjectTags.getString("sectionId");
                    String tagSectionName = jsonObjectTags.getString("sectionName");
                    // iam just get 4 string ! you can get all tags string with replacing
                }
                //** and get others  each jsonObject from result Array
            }



        }
        catch (JSONException e)
        {
            e.printStackTrace();
            // when parse json error happend! check log  cat 
        }

此代码解析此链接

我只从每个部分中提取了一些项目来澄清情况

于 2018-01-13T06:45:29.937 回答