2017-06-24 27 views
1

我想將一個JSON數組放入java中的var中。我的JSON似乎確定,但是當我嘗試將一個JSOn數組放入一個java數組var時,它不起作用。 這裏我得到的錯誤:errororg.json.JSONException:JSONObject [「tweets」]不是JSONArray。從DBObject讀取一個屬性

MongoClient mongoClient = new MongoClient("localhost" , 27017); 
    DB db = mongoClient.getDB("test1"); 


    DBCollection coll = db.getCollection("tweetsCol"); 
    DBCursor cursor = coll.find(); 

    while (cursor.hasNext()) { 

     BasicDBObject obj = (BasicDBObject) cursor.next(); 
     JSONObject objjj = new JSONObject(obj); 
     try{ 
      System.out.println("okok "+objjj); // THE JSON I WILL SHOW YOU 

      JSONArray jsonMainArr = objjj.getJSONArray("tweets"); 

     }catch(JSONException e){ 
      System.out.println("error"+e); 
     }} 

這裏是我的數據在MongoDB中:

{ 
    "_id":"5939bc6676abbe186feb73a5", 
    "user_request_id":"5941903f37aaa6ec55689e85", 
    "tweets":[ 
     { 
     "date":"Wed Jun 07 18:32:57 CDT 2017", 
     "text":"[Earthview Wonders][Video] No.265: Astronaut Thomas Pesquet completed 6-month #MissionProxima. #Neweyes\u2026 ", 
     "_id":872597276891398144, 
     "user":"livearthjp" 
     }, 
     { 
     "date":"Wed Jun 07 18:16:56 CDT 2017", 
     "text":"Astronaut Thomas Pesquet @Thom_astro Shares His #Songs4Space ", 
     "_id":872593245716467712, 
     "user":"anasia5mice" 
     }, 
     { 
     "date":"Wed Jun 07 15:46:03 CDT 2017", 
     "text":"Thomas Pesquet: Undocking and landing ", 
     "_id":872555275387117570, 
     "user":"GRASSIFREE" 
     }, 
     { 
     "date":"Wed Jun 21 17:02:37 CDT 2017", 
     "text":"@Thom_astro @Space_Station And his colleagues said, 'Pesquet, if you play Baker Street one more time...'", 
     "_id":877647972430823429, 
     "user":"kimkemmis" 
     }, 
     { 
     "date":"Wed Jun 21 17:01:16 CDT 2017", 
     "text":"[News] ", 
     "_id":877647632524394497, 
     "user":"ArthurC2Pouce" 
     }, 
     { 
     "date":"Wed Jun 21 11:28:48 CDT 2017", 
     "text":"Thomas Pesquet's music is OUT THERE! Cool dude. ", 
     "_id":877563967178104836, 
     "user":"tiarudd34" 
     }, 
     { 
     "date":"Wed Jun 21 11:10:15 CDT 2017", 
     "text":"jaime thomas pesquet", 
     "_id":877559296741048320, 
     "user":"sosthene_maus" 
     }, 
     { 
     "date":"Wed Jun 21 10:23:03 CDT 2017", 
     "text":"French astronaut Thomas Pesquet took some of the most amazing pictures ever while in spce ", 
     "_id":877547418606329861, 
     "user":"raygibbs1" 
     }, 
     { 
     "date":"Wed Jun 21 10:23:00 CDT 2017", 
     "text":"French astronaut Thomas Pesquet shares stunning pictures of Earth: via @AOL", 
     "_id":877547405180157952, 
     "user":"raygibbs1" 
     }, 
     { 
     "date":"Wed Jun 21 08:46:13 CDT 2017", 
     "text":"Coll Cambuston like Thomas Pesquet! @thomastro @cardierun", 
     "_id":877523048546676736, 
     "user":"CambF974" 
     }, 
     { 
     "date":"Wed Jun 21 08:00:06 CDT 2017", 
     "text":"Thomas Pesquet returned to Earth on 2 June 2017 after completion of his six-months long Proxima mission to the... ", 
     "_id":877511443775619072, 
     "user":"rospaceagency" 
     }, 
     { 
     "date":"Tue Jun 20 23:50:34 CDT 2017", 
     "text":"Thomas Pesquet @Thom_astro and Messier 83 #Astronauts #ESA @CNES #NASA ✨\u200d✨ ", 
     "_id":877388248368033794, 
     "user":"AmirAliBehrooz" 
     } 
    ], 
    "query_name":"Pesquet", 
    "active":"true", 
    "started_at":"2017_06_08" 
} 
+0

這不是JSON。它就像你的代碼實際定義的['BasicDBObject'](http://api.mongodb.com/java/current/com/mongodb/BasicDBObject.html)。相反,您可以使用可用的方法訪問屬性,而不是使用JSONObject'。他們是兩個不同的東西。 –

+0

只需'obj.get(「tweets」)'。不需要JSON轉換。 –

回答

0

現在我更好地瞭解您的問題感謝@Neil倫恩,並按照他的最好的建議,你要明白,你不和JSON在這裏處理,你可以直接做:

MongoClient mongoClient = new MongoClient("localhost" , 27017); 
    DB db = mongoClient.getDB("test1"); 


    DBCollection coll = db.getCollection("tweetsCol"); 
    DBCursor cursor = coll.find(); 

    while (cursor.hasNext()) { 

     BasicDBObject obj = (BasicDBObject) cursor.next(); 
     try{ 
      System.out.println("okok "+obj); // THE JSON I WILL SHOW YOU 

      BasicDBList jsonMainArr = obj.get("tweets"); 

     }catch(JSONException e){ 
      System.out.println("error"+e); 
     }} 
+0

這不是JSON。 OP是錯誤的並且做錯了。已經有一個訪問者作爲'BasicDBObject'是一個抽象的'Map' –

+0

@NeilLunn啊..我明白了,謝謝你清理 –

+0

問題是由於誤解而引起的誤解。感謝漂亮的打印數據。 –