2016-12-22 23 views
0

は、私がフィールドオブジェクトから署名欄を取得しようとしている私の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と私はそれを持っているように滞在したいと思います。私はちょうどその入れ子になったオブジェクトを取得する方法を理解していません。助けてくれてありがとう!

+0

あなたはそれではJavaScriptやJavaで行わ構文解析を取得したいですか? –

答えて

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(); 
     } 

    } 

} 
関連する問題