android - 如何在 Guardian API 中解析 java 中 json 对象中的 json 对象?
问题描述
我想为我的应用程序从 API the Guardian news 解析 JSON 数据到 Java Android。JSON 数据是 JsonObject 中的 JsonObject。我必须尝试几种方法,但仍然没有返回数据。
JSON数据
{
"response":{
"status":"ok",
"userTier":"developer",
"total":1,
"content":{
"id":"us-news/2019/sep/09/us-briefing-taliban-talks-brexit-turmoil-and-nadal-wins-us-open",
"type":"article",
"sectionId":"us-news",
"sectionName":"US news",
"webPublicationDate":"2019-09-09T10:25:05Z",
"webTitle":"US briefing: Taliban talks, Brexit turmoil and Nadal wins US Open",
"webUrl":"https://www.theguardian.com/us-news/2019/sep/09/us-briefing-taliban-talks-brexit-turmoil-and-nadal-wins-us-open",
"apiUrl":"https://content.guardianapis.com/us-news/2019/sep/09/us-briefing-taliban-talks-brexit-turmoil-and-nadal-wins-us-open",
"fields":{
"headline":"US briefing: Taliban talks, Brexit turmoil and Nadal wins US Open",
"standfirst":"<p>Monday’s top story: Taliban warn of further US deaths after Trump says he cancelled peace talks. Plus, why the breakup of Big Tech is beginning at last</p>",
"trailText":"Monday’s top story: Taliban warn of further US deaths after Trump says he cancelled peace talks. Plus, why the breakup of Big Tech is beginning at last",
"byline":"Tim Walker",
"main":"<figure class=\"element element-image\" data-media-id=\"9c05cc7c0e69a78aa3f71f94a4bdd28e3d044a48\"> <img src=\"https://media.guim.co.uk/9c05cc7c0e69a78aa3f71f94a4bdd28e3d044a48/0_124_5610_3364/1000.jpg\" alt=\"Afghans at the site of a deadly car bomb attack in Kabul last week.\" width=\"1000\" height=\"600\" class=\"gu-image\" /> <figcaption> <span class=\"element-image__caption\">Afghans at the site of a deadly car bomb attack in Kabul last week.</span> <span class=\"element-image__credit\">Photograph: Rahmatullah Alizadah/Xinhua/Barcroft Media</span> </figcaption> </figure>",
"body":"<p><em><a href=\"https://www.theguardian.com/info/2018/sep/17/guardian-us-morning-briefing-sign-up-to-stay-informed\">Subscribe now</a> to receive the morning briefing by email.</em></p> <p>Good morning, I’m Tim Walker with today’s essential stories.</p> <h2>Republicans and Democrats denounce Taliban talks</h2> <p>Lawmakers from both sides of the aisle have criticised Donald Trump after <a href=\"https://www.theguardian.com/us-news/2019/sep/08/donald-trump-says-he-was-due-to-host-taliban-at-camp-david\">he revealed </a><a href=\"https://www.theguardian.com/us-news/2019/sep/08/donald-trump-says-he-was-due-to-host-taliban-at-camp-david\">he had planned a secret peace summit</a> with the Taliban at Camp David on Sunday, <a href=\"https://www.theguardian.com/us-news/2019/sep/08/donald-trump-cancelled-taliban-talks-camp-david-typical-hot-cold\">only to call it off at the last minute</a>. ",
"wordcount":"1172",
"commentCloseDate":"2019-09-12T10:30:00Z",
"commentable":"false",
"firstPublicationDate":"2019-09-09T10:25:05Z",
"isInappropriateForSponsorship":"false",
"isPremoderated":"false",
"lastModified":"2019-09-09T10:48:23Z",
"productionOffice":"UK",
"publication":"theguardian.com",
"shortUrl":"https://gu.com/p/ca9ye",
"shouldHideAdverts":"false",
"showInRelatedContent":"true",
"thumbnail":"https://media.guim.co.uk/9c05cc7c0e69a78aa3f71f94a4bdd28e3d044a48/0_124_5610_3364/500.jpg",
"legallySensitive":"false",
"lang":"en",
"bodyText":"Subscribe now to receive the morning briefing by email. Good morning, I’m Tim Walker with today’s essential stories.",
"charCount":"7006",
"shouldHideReaderRevenue":"false",
"showAffiliateLinks":"false",
"bylineHtml":"<a href=\"profile/walker-tim\">Tim Walker</a>"
},
"isHosted":false,
"pillarId":"pillar/news",
"pillarName":"News"
}
}
}
我想循环内容数据webTitle
,fields
并body
在fields
.
谢谢,提前。
解决方案
我找到了解决方案
// Create a JSONObject from the JSON response string
JSONObject baseJsonResponse = new JSONObject(result);
//Create the JSONObject with the key "response"
JSONObject responseJSONObject = baseJsonResponse.getJSONObject("response");
// Extract the JSONArray associated with the key called "content",
// which represents a list of news stories.
JSONObject newsContentArray = responseJSONObject.getJSONObject("content");
Iterator<String> keys = newsContentArray.keys();
while(keys.hasNext()) {
String key = keys.next();
if (newsContentArray.get(key) instanceof JSONObject) {
Object webTitle = newsContentArray.get("webTitle");
Object webPublicationDate = newsContentArray.get("webPublicationDate");
JSONObject fields = newsContentArray.getJSONObject("fields");
String body = fields.getString("body");
String main = fields.getString("main");
setTextNews(webTitle.toString(), webPublicationDate.toString(), main, body);
}
}
推荐阅读
- haskell - 在不是函子的东西上使用镜头
- c# - 在 ColdFusion 中将变量传递给 .NET dll
- angular - Angular 7 响应式下拉菜单
- ios - 文件“Pods-xxx.xcconfig”无法打开,因为没有这样的文件:/Pods 在我的配置路径中出现两次
- javascript - 如果在 React-Select v2 中搜索时没有剩余选项,如何创建新选项?
- python - 线性回归:ValueError:除连接轴外的所有输入数组维度必须完全匹配
- haskell - LiquidHaskell 与 Idris 中的运行时“类型术语”
- php - PHP查询依赖于填写的表单
- c - 访问 C 向量中的写入冲突
- php - 无法从 else 条件中获取字段