需要通过java代码将.json文件插入MongoDB

时间:2016-04-04 13:01:21

标签: java c# json mongodb

我是初学者。我运行了MongoDB。我的任务是通过java代码而不是mongoimport Student.json 文件插入MongoDB。

Student.java

public class Student {

@Id
private ObjectId Id;

private long studentId;
private String studentName;
private String qualification;

public Student(){

}

public Student(long studentId, String studentName, String qualification) {
    this.studentId = studentId;
    this.studentName = studentName;
    this.qualification = qualification;
}

public long getStudentId() {
    return studentId;
}

public void setStudentId(long studentId) {
    this.studentId = studentId;
}

public String getStudentName() {
    return studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

public String getQualification() {
    return qualification;
}

public void setQualification(String qualification) {
    this.qualification = qualification;
}
}

Student.json

[{
     "studentId": 1,
     "studentName": "Shreyas",
     "qualification": "B.E"
},
{
     "studentId": 2,
     "studentName": "Yashas",
     "qualification": "B.Tech"
}]

UpdateSudentModel.java

public class UpdateStudentModel {

private static UpdateStudentModel USM;
private StudentRepo sr;


public static void main(String[] args) {
    // TODO Auto-generated method stub

    try{

        File file = new File("/home/bshreyasrao/Student.json");


        Injector injector = Guice.createInjector(new BindingModules());
        StudentRepo sr = injector.getInstance(StudentRepo.class);

        USM = new UpdateStudentModel(sr);
        USM.importFromJsonToMongoDB(file);
        } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error while injecting/File is not present");
        }       
}

public UpdateStudentModel(StudentRepo sr)
{
    this.sr = sr;
}

public void importFromJsonToMongoDB(File file){

    try{
        JsonParser parser = new JsonFactory().createParser(file);
        ObjectMapper mapper = new ObjectMapper();
        Iterator<Student> iterator = mapper.readValues(parser, Student.class);

         while(iterator.hasNext()) {
             sr.save(iterator.next());
            }
    }catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error While parsing data");
    }
}
}

错误

com.fasterxml.jackson.databind.RuntimeJsonMappingException: Can not deserialize instance of com.shreyas.student.model.Student out of START_ARRAY token
 at [Source: /home/bshreyasrao/Student.json; line: 1, column: 1]
    at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:194)
    at com.shreyas.student.UpdateStudentModel.importFromJsonToMongoDB(UpdateStudentModel.java:55)
    at com.shreyas.student.UpdateStudentModel.main(UpdateStudentModel.java:34)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.shreyas.student.model.Student out of START_ARRAY token
 at [Source: /home/bshreyasrao/Student.json; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:216)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1293)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:159)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)
    at com.fasterxml.jackson.databind.MappingIterator.nextValue(MappingIterator.java:277)
    at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:192)
    ... 2 more
Error While parsing data

我尝试过的事情

  • Student.json 中,如果我删除&#34; [&#34; ,&#34;]&#34;和&#34;,&#34;它分隔了2个json对象,然后上面的代码 UpdateStudentModel.java 工作正常。

{"studentId": 1,"studentName": "Shreyas","qualification": "B.E"} {"studentId": 2,"studentName": "Yashas","qualification": "B.Tech"}

我知道这不是有效的JSON格式。

  • 因此,为了成功使用有效的JSON文件,我应该如何处理这些&#34; [&#34; ,&#34;]&#34;和&#34;,&#34;

  • 我应该在代码中做些什么改变?....请帮助我解决这个问题..

1 个答案:

答案 0 :(得分:0)

在你的代码中,你期待一个Student对象。但是你的JSON是一个学生数组。您是否可以尝试将代码更改为期望JSON数组?

更改此行:

Iterator<Student> iterator = mapper.readValues(parser, Student.class);

如下:

Student[] iterator = mapper.readValues(parser, Student[].class);

OR

List<Student> iterator = mapper.readValues(parser, new TypeReference<List<Student>>.class);