在Java WebServlet中,我正在读取文件:
[
{"firstName":"Chelsea","surname":"Ganter","dob":"29/03/2005","scoutNumber":"181812","joiningDate":"01/09/2016"},
{"firstName":"Oliver","surname":"Greenhatch","dob":"10/09/2007","scoutNumber":"268981","joiningDate":"17/07/2019"}
]
使用:
String[] myJsonData = request.getParameterValues("memberList");
我试图找出如何读取对象并将其写入数据库的方法,但是我无法使其正常工作(例如How to parse a JSON and turn its values into an Array?)。
我添加时:
try {
JSONObject myjson = new JSONObject(myJsonData.toString());
JSONArray the_json_array = myjson.getJSONArray("profiles");
int size = the_json_array.length();
ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = the_json_array.getJSONObject(i);
System.out.println(another_json_object);
//Blah blah blah...
arrays.add(another_json_object);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我在打开应用程序之前收到404错误。
根据我的理解,如果您希望通过将值保存在某个实体类中来将值保存到数据库中,则可以将JSON数据直接加载到该类中,我创建了一个示例类,名为profile使用@JsonPoperty
可以更改的相同字段。在示例代码中,日期字符串将自动转换为日期,从而隐藏了实现并简化了工作。下面是示例代码:-
import java.util.Arrays;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SOTest {
public static void main(String[] args) {
try {
String inputJson = "[\r\n" +
"{\"firstName\":\"Chelsea\",\"surname\":\"Ganter\",\"dob\":\"29/03/2005\",\"scoutNumber\":\"181812\",\"joiningDate\":\"01/09/2016\"},\r\n" +
"{\"firstName\":\"Oliver\",\"surname\":\"Greenhatch\",\"dob\":\"10/09/2007\",\"scoutNumber\":\"268981\",\"joiningDate\":\"17/07/2019\"}\r\n" +
"]";
com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
Profile[] profiles = mapper.readValue(inputJson, Profile[].class);
System.out.println(Arrays.asList(profiles));
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Profile{
private String firstName;
private String surname;
@JsonFormat(pattern="dd/MM/yyyy")
private Date dob;
private int scoutNumber;
@JsonFormat(pattern="dd/MM/yyyy")
private Date joiningDate;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public int getScoutNumber() {
return scoutNumber;
}
public void setScoutNumber(int scoutNumber) {
this.scoutNumber = scoutNumber;
}
@Override
public String toString() {
return "Profile [firstName=" + firstName + ", surname=" + surname + ", dob=" + dob + ", scoutNumber="
+ scoutNumber + ", joiningDate=" + joiningDate + "]";
}
}
输出(仅打印ArrayList元素):-
Profile [firstName=Chelsea, surname=Ganter, dob=Tue Mar 29 05:30:00 IST 2005, scoutNumber=181812, joiningDate=Thu Sep 01 05:30:00 IST 2016],
Profile [firstName=Oliver, surname=Greenhatch, dob=Mon Sep 10 05:30:00 IST 2007, scoutNumber=268981, joiningDate=Wed Jul 17 05:30:00 IST 2019]