java - 从 DBObject 读取属性

标签 java mongodb

我想将 JSON 数组放入 java 中的 var 中。我的 JSON 看起来没问题,但是当我尝试将 JSOn 数组放入 java 数组 var 时,它不起作用。 这里我得到的错误: errororg.json.JSONException: JSONObject["tweets"] is not a JSONArray.

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"
}

最佳答案

现在,感谢@Neil Lunn,我更好地理解了你的问题,并遵循他的最佳建议,你必须明白你不是在这里处理 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);
        }}

关于java - 从 DBObject 读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44732345/

相关文章:

java - Android:BufferedReader 第二次后返回错误数据

mongodb - 针对计数器字段构建索引

node.js - Expressjs + Mongoose - 该网页不可用?

javascript - 蒙戈错误: No transaction started

mongodb - Ubuntu 12.04下存储扩展后MongoDB服务无法启动

java - Java 中继承的最终常量值?

java - 加特林每次调用的负载

java - 用于 CUCM 多集群的 JTAPI

java - BIRT 中的联合数据集仅限于两个表

php - 安装失败-El Capitan上PHP的mongodb驱动