【问题标题】:How can I get a value from a nested object?如何从嵌套对象中获取值?
【发布时间】: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 解析它?

标签: java json parsing object


【解决方案1】:

这是使用JSON.simple 库的解决方案:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Hello {

    public static void main(String[] args) {

        String s = "{" + 
                "\"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" +
                "   }]" +
                "}" +
                "}";

        JSONParser parser = new JSONParser();
        try {
            JSONObject obj = (JSONObject)parser.parse(s);

            obj = (JSONObject) obj.get("response");
            JSONArray arr = (JSONArray) obj.get("results");
            obj = (JSONObject) arr.get(0); //assuming you want the first record of the array
            obj = (JSONObject) obj.get("fields");
            System.out.println(obj.get("byline"));

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

    }

}

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 2020-02-08
    • 1970-01-01
    • 2013-10-11
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多