如何从嵌套对象中获取值?

时间:2016-12-22 20:21:37

标签: java json parsing object

这是我的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");
&#13;
&#13;
&#13;

我真的希望以我拥有的方式使用JSON。我只是不明白如何获得嵌套对象。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

以下是使用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();
        }

    }

}