gpt4 book ai didi

java - 从 DBObject 读取属性

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:45 27 4
gpt4 key购买 nike

我想将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com