2

I have following response from the mongodb and i can't find a way to desearialize it .

[{ "_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
}]

More specifically , i can't find a way to parse the $date element and $oid .How do i write a data member corresponding to that element or do i have to write an inner class? it may seem very basic question but i couldn't find a way .i will use that class in gson parse to parse .Thanks

JCOC611
  • 19,111
  • 14
  • 69
  • 90
Ranger
  • 47
  • 5
  • I'm a little confused by your question -- are you trying to access the oid or date field in your Java program? Which step are you having trouble with ? The _id field is set to an embedded document of { "$oid" : "some ids"}, which is itself just a BSON object, so to get the contents of the $oid field you could call the following : document.get( "_id" ).get( "$oid" ). Is this what you're asking about? – Louisa Oct 15 '12 at 15:18
  • i understand what you are trying to say but i am using this code on Android and for this platform we don't have mongodb Library. so we have to use the json parsing to get the information out of JSON object.I am using Gson which required a mapping class to get the object and i am having problem to get BSON object _oid and $date. – Ranger Oct 18 '12 at 13:21

2 Answers2

0

In the toString, $date and $oid are artificial fields. If you're using the standard java mongo driver, then get("created_at") should be a java.util.Date instance, and get("_id") should be an org.bson.types.ObjectId instance.

I'm a bit confused by your question, so if you are instead working with raw BSON objects, then "created_at" will be a BSON Date type (0x09), whose payload is a long representing millis since the Unix Epoch. The "_id" will be a BSON ObjectId type (0x07) with a payload of 12 bytes for the id. If I recall correctly, it's opposite endian from what Java expects. See the BSON Spec for more details.

Fuwjax
  • 2,327
  • 20
  • 18
0

I think i have come up with the solution , partially i got the idea from this and for the mapping class i had to use annotations with _id and created_at. Also i had to make static inner class for each _id and created_at . code is given below , which is working by now but i think there is more elegant solution out there so i am keeping this question open if someone have better solution else will close in some time

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

    }
Community
  • 1
  • 1
Ranger
  • 47
  • 5