2014-02-19 17 views
0

我要得到Facebook的閱讀Java中的JSON文件read_books 文件的格式如下:如何使用org.json.simple包

{ 
    "data": [ 
     { 
     "id": "270170479804513", 
     "from": { 
      "name": "L I", 
      "id": "1000022" 
     }, 
     "start_time": "2014-01-22T09:31:00+0000", 
     "publish_time": "2014-01-22T09:31:00+0000", 
     "application": { 
      "name": "Books", 
      "id": "174275722710475" 
     }, 
     "data": { 
      "book": { 
       "id": "133556360140246", 
       "url": "https://www.facebook.com/pages/Pride-and-Prejudice/133556360140246", 
       "type": "books.book", 
       "title": "Pride and Prejudice" 
      } 
     }, 
     "type": "books.reads", 
     "no_feed_story": false, 
     "likes": { 
      "count": 0, 
      "can_like": true, 
      "user_likes": false 
     }, 
     "comments": { 
      "count": 0, 
      "can_comment": true, 
      "comment_order": "chronological" 
     } 
     }, 
     { 
     "id": "270170328", 
     "from": { 
      "name": "h", 
      "id": "100004346894022" 
     }, 
     "start_time": "2014-01-22T09:29:42+0000", 
     "publish_time": "2014-01-22T09:29:42+0000", 
     "application": { 
      "name": "Books", 
      "id": "174275722710475" 
     }, 
     "data": { 
      "book": { 
       "id": "104081659627680", 
       "url": "https://www.facebook.com/pages/Gulistan-of-Sadi/104081659627680", 
       "type": "books.book", 
       "title": "Gulistan of Sa'di" 
      } 
     }, 
     "type": "books.reads", 
     "no_feed_story": false, 
     "likes": { 
      "count": 0, 
      "can_like": true, 
      "user_likes": false 
     }, 
     "comments": { 
      "count": 0, 
      "can_comment": true, 
      "comment_order": "chronological" 
     } 
     } 
    ], 

我需要書的標題和網頁的網址。我運行下面的代碼,但我得到了線程「主要」 java.lang.ClassCastException異常:org.json.simple.JSONObject不能轉換爲java.lang.String

while ((inputLine = in.readLine()) != null) 
    { 
    s = s + inputLine + "\r\n"; 
    if (s == null) { 
    break; 
    } 
    t = t + inputLine + "\r\n"; 
    } 
    in.close(); 
    t = t.substring(0, t.length() - 2); 
    System.out.println(t); 
    Object dataObj =JSONValue.parse(t); 

System.out.println(dataObj); 
JSONObject dataJson = (JSONObject) dataObj; 
JSONArray data = (JSONArray) dataJson.get("data"); 
for (Object o: data) 
{ 
    JSONObject indata= (JSONObject) o; 
    Object indatafirst=(JSONObjec`enter code here`t).get("0"); 
    String inndata=(String) indatafirst.get("data"); 
    System.out.println("inndata"+inndata); 
}} 

,但事實並非如此

+0

什麼 「是不是真的?」 – qqilihq

+0

JSON中沒有'read_books'字段...你是什麼意思? –

回答

1

問題是與下面的行:

String inndata=(String) indatafirst.get("data");

data字段在JSON是不是字符串,它是一個嵌套的JSON對象。

"data": { 
      "book": { 
       "id": "104081659627680", 
       "url": "https://www.facebook.com/pages/Gulistan-of-Sadi/104081659627680", 
       "type": "books.book", 
       "title": "Gulistan of Sa'di" 
      } 
} 

這解釋了您的ClassCastException。

相反,你應該這樣做:

JSONObject data = (JSONObject) indatafirst.get("data"); 
JSONObject book = (JSONObject) data.get("book"); 
String bookTitle = book.get("title");