I am working with android studio and I want to retrieve the data from the database mongodb. So I have built the connection and I can get the data in JSON successfully. But when I want to iterate it, it doesn't work.
ArrayList<DatabaseSchema> user = new ArrayList<DatabaseSchema>();
String mongoarray = "{ db_list: " + list + "}";
Object o = com.mongodb.util.JSON.parse(mongoarray);
DBObject dbObj = (DBObject) o;
BasicDBList userList = (BasicDBList) dbObj.get("db_list");
for (Object obj : userList) {
DBObject userObj = (DBObject) obj;
DatabaseSchema temp = new DatabaseSchema();
temp.setUsername(userObj.get("username").toString());
user.add(temp);
}
And at the end of this method I want to return the user. But when I want to retrieve the username of this user I get nothing and there is no error.
EDIT:
public class DatabaseSchema {
public String username;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}
and I retrieve this input:
[ { "_id" : { "$oid" : "586.."} , "document" : { "username" : "gmz" } } ]