无法从mongodb解析json格式的文档列表

时间:2012-10-13 04:47:43

标签: java mongodb gson

我有以下mongodb的回应,我无法找到一种方法来去除它。

[{ "_id" : { "$oid" : "some ids"} ,
   "index" : 0 , 
   "question" : "some text ?",
   "optiona" : "OS",
   "optionb" : "JAVA",
   "optionc" : "C",
   "optiond" : "C#",
   "answer" : "JAVA",
   "created_at" : { "$date" : "2012-09-20T06:37:04.306Z" },
   "Active" : "1"
 }, 
 { "_id" : { "$oid" : "505ab997aded66f4c1ccc7f3" }, 
   "index" : 1 ,
   ..../objects like that
}]

更具体地说,我找不到解析$ date元素和$ oid的方法。如何编写与该元素对应的数据成员,还是必须编写内部类?它似乎是非常基本的问题,但我找不到办法。我将在gson解析中使用该类来解析。谢谢

2 个答案:

答案 0 :(得分:0)

在toString中,$ date和$ oid是人工字段。如果您使用的是标准的java mongo驱动程序,那么get(“created_at”)应该是java.util.Date实例,而get(“_ id”)应该是org.bson.types.ObjectId实例。

我对你的问题感到有点困惑,所以如果你正在使用原始BSON对象,那么“created_at”将是BSON Date类型(0x09),其有效载荷是一个长的,表示自Unix Epoch以来的毫秒数。 “_id”将是BSON ObjectId类型(0x07),id的有效负载为12字节。如果我没记错的话,它与Java期望的结尾相反。有关详细信息,请参阅BSON Spec

答案 1 :(得分:0)

我想我已经提出了解决方案,部分我从this获得了想法,对于映射类,我必须使用注释 _id created_at 。另外,我必须为每个_id和created_at创建静态内部类。代码在下面给出,现在正在工作,但我认为有更优雅的解决方案,所以我保持这个问题,如果有人有更好的解决方案,其他将在一段时间内关闭

          import com.google.gson.annotations.SerializedName;
          import java.sql.Date;



         public class QuizDBObject {

/**
 * this class will work as the place holder for the quiz object.
 * An extra variable is added to store answer given by user.
 */
@SerializedName("_id")
 Id _id;



public Id get_id() {
    return _id;
}
public void set_id(Id _id) {
    this._id = _id;
}
@SerializedName("index")

private long index ;
@SerializedName("question")

private String question;
@SerializedName("answer")

private String answer;
@SerializedName("optiona")

private String optiona;
@SerializedName("optionb")

private String optionb;
@SerializedName("optionc")

private String optionc;
@SerializedName("optiond")

private String optiond;
@SerializedName("created_at") 
MyDate created_at;

    public MyDate getCreated_at() {
    return created_at;
}
public void setCreated_at(MyDate created_at) {
    this.created_at = created_at;
}

public void setOptiona(String optiona) {
    this.optiona = optiona;
}
@SerializedName("Active")
private String Active;
/**
 * for storing the answer given by the user for every question.
 */
private String userAnswer;



public String getUserAnswer() {
    return userAnswer;
}
public void setUserAnswer(String userAnswer) {
    this.userAnswer = userAnswer;
}
public QuizDBObject(){
    super();
}

public long getIndex() {
    return index;
}
public void setIndex(long index) {
    this.index = index;
}
public String getQuestion() {
    return question;
}
public void setQuestion(String question) {
    this.question = question;
}
public String getAnswer() {
    return answer;
}
public void setAnswer(String answer) {
    this.answer = answer;
}
public String getOptiona() {
    return optiona;
}
public void setOptionA(String optiona) {
    this.optiona = optiona;
}
public String getOptionb() {
    return optionb;
}
public void setOptionb(String optionb) {
    this.optionb = optionb;
}
public String getOptionc() {
    return optionc;
}
public void setOptionc(String optionc) {
    this.optionc = optionc;
}
public String getOptiond() {
    return optiond;
}
public void setOptiond(String optiond) {
    this.optiond = optiond;
}

public String getActive() {
    return Active;
}
public void setActive(String Active) {
    this.Active = Active;
}
/*public void setCreatedAtString(String type) {
    this.created_at.parse(type);

}
*/

static class Id{
    String $id;

    public String get$id() {
        return $id;
    }

    public void set$id(String $id) {
        this.$id = $id;
    }

}
static class MyDate{
    String $date;

    public String get$date() {
        return $date;
    }

    public void set$date(String $date) {
        this.$date = $date;
    }
}

    }