2016-06-27 5 views
0

私はライブラリJSONを使用してJSONファイルを解析しようとしています。最初の2つの文字列は良いです。しかし、私がオブジェクトの社会を通して解析しようとすると、facebook: null,Pinterest : null、およびrss: nullが得られます。 2番目のオブジェクトを解析するにはどうすればよいですか?こんにちは、私は、JavaでJSONファイルを解析する助けが必要です

がここにここに私のJSONファイル

{ 
    "blogURL": "www.sheriyBegin", 
    "twitter": "http://twitter.com/Sherily", 
    "social": { 
     "facebook": "http://facebook.com/Sherily", 
     "pinterest": "https://www.pinterest.com/Sherily/Sherily-articles", 
     "rss": "http://feeds.feedburner.com/Sherily" 
    } 
} 

である私は

package javaugh; 

import java.io.FileReader; 

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


public class JavaUgh { 


    public static void main(String[] args)throws Exception{ 
     JSONParser parser = new JSONParser(); 

     Object obj = parser.parse(new FileReader("C:\\Users\\HP\\Desktop\\Research\\file2.txt")); 
     JSONObject jsonObject = (JSONObject) obj; 

     String blog = (String)jsonObject.get("blogURL"); 
     String twitter = (String)jsonObject.get("twitter"); 

     JSONObject jsonObject2 = (JSONObject) obj; 
     String face = (String)jsonObject2.get("facebook"); 
     String pin = (String)jsonObject2.get("pinterest"); 
     String rss = (String)jsonObject2.get("rss"); 



     System.out.println("Blog: "+ blog); 
     System.out.println("Twitter Page : " + twitter); 
     System.out.println("Socail:"); 
     System.out.println("Facebook Page : " + face); 
     System.out.println("Pintersect: " + pin); 
     System.out.println("Rss : " + rss); 


    } 

} 

を書いたコードで出力:

Blog: www.sheriyBegin 
Twitter Page : http://twitter.com/Sherily 
Socail: 
Facebook Page : null 
Pintersect: null 
Rss : null 

答えて

1

あなたが最初の1から2番目のJSONオブジェクトを取得する必要があり、最初のものへの新しい参照を作成することではありません。

JSONObject jsonObject2 = (JSONObject) jsonObject.get("social"); 
+0

おかげで、それが働きました。迅速な対応のため、ありがとうございます。 – user3251123

+0

@Pshemoその編集をありがとう – user3251123

0
JSONObject social = (JSONObject) jsonObject.get("social"); 
String face = (String)social.get("facebook"); 
String pin = (String)social.get("pinterest"); 
String rss = (String)social.get("rss"); 
関連する問題