gpt4 book ai didi

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

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

我是初学者。我正在运行 MongoDB。我的任务是通过 java 代码而不是通过 mongoimportStudent.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 中,如果我删除分隔 2 个 json 对象的“[”、“]”和“,”,则上面的代码 UpdateStudentModel.java 可以正常工作。

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

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

  • 那么为了成功使用有效的 JSON 文件,我应该如何处理这些“[”、“]”和“,”?

  • 我应该在代码中做哪些更改?....请在这方面帮助我..

最佳答案

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

更改此行:

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

如下:

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

或者

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

关于java - 需要通过java代码将.json文件插入MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403439/

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