【发布时间】:2016-12-22 20:21:37
【问题描述】:
这是我的 JSON
{
"response":{
"status":"ok",
"userTier":"developer",
"total":57113,
"startIndex":1,
"pageSize":10,
"currentPage":1,
"pages":5712,
"orderBy":"newest",
"results":[
{
"id":"technology/2016/dec/22/google-profiting-holocaust-denial-jewish-breman-museum",
"type":"article",
"sectionId":"technology",
"sectionName":"Technology",
"webPublicationDate":"2016-12-22T19:04:12Z",
"webTitle":"Google is profiting from Holocaust denial, says Jewish museum",
"webUrl":"https://www.theguardian.com/technology/2016/dec/22/google-profiting-holocaust-denial-jewish-breman-museum",
"apiUrl":"https://content.guardianapis.com/technology/2016/dec/22/google-profiting-holocaust-denial-jewish-breman-museum",
"fields":{
"byline":"Carole Cadwalladr"
},
"isHosted":false
},
我正在尝试从字段对象中获取署名。我运行下面的代码没有错误,它编译得很好,但给出了一个错误,说“没有字段值”,如果我删除了字段的东西,它对其他所有东西都运行良好。
try {
JSONObject baseJsonResponse = new JSONObject(newsJSON);
JSONObject parentObject = baseJsonResponse.getJSONObject("response");
JSONArray newsArray = parentObject.getJSONArray("results");
JSONObject fields;
for (int i = 0; i < newsArray.length(); i++) {
JSONObject currentNews = newsArray.getJSONObject(i);
fields = currentNews.getJSONObject("fields");
String title = currentNews.getString("webTitle");
String author = fields.getString("byline");
String time = currentNews.getString("webPublicationDate");
String url = currentNews.getString("webUrl");
我真的很想按照我的方式继续使用 JSON。我只是不明白如何获取该嵌套对象。感谢您的帮助!
【问题讨论】:
-
你想用 Javascript 还是 Java 解析它?