如何将 JSON 数据插入数据库
How to Insert JSON data into database
从我的 UI 通过 ajax 我得到一个 JSON 数据,我正试图将其插入到我的数据库中,我不知道我该怎么做,我知道如何使用 prepared statements
在数据库中插入一些东西,但是在 JSON 的情况下我完全感到困惑
这是我的JSON,我在我的 doPost
{"ImageData":[{"Counter":"Counter A","Name":"CountA1.jpg","IsActive":"Y"},{"Counter":"Counter A","Name":"CountA2.jpg","IsActive":"Y"}]}
servlet 代码 -->
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Scanner scanner = new Scanner(request.getInputStream());
String imageData = scanner.nextLine();
System.out.println(imageData);
}
现在我想像
一样将其存储在我的数据库中
我需要解析这个吗JSON,我该怎么做ps.setInt(index, 0);
,我不知道
Edit/update
作为@Anjana Senanayake 回答我是这样做的
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Scanner scanner = new Scanner(request.getInputStream());
String imageData = scanner.nextLine();
System.out.println(imageData);
JSONObject jsonObj = new JSONObject(imageData);
JSONArray jsonArray = jsonObj.getJSONArray("ImageData");
JSONObject innerObj = new JSONObject(jsonArray); // this one is throwing error
// eror is `The type of the expression must be an array type but it resolved to JSONArray`
String counter = innerObj.getString("Counter");
String name = innerObj.getString("Name");
String isActive = innerObj.getString("IsActive");
System.out.println(counter);
}
但它在 JSONObject innerObj = new JSONObject(jsonArray[0]);
处抛出错误,错误是 The type of the expression must be an array type but it resolved to JSONArray
使用org.json库解析它并创建JsonObject :-
JSONObject jsonObj = new JSONObject(imageData);
JSONArray jsonArray = jsonObj.getJSONArray("ImageData");
现在,使用此对象获取您的值:-
JSONObject innerObj = new JSONObject(jsonArray[0]);
String counter = innerObj.getString("Counter");
String name = innerObj.getString("Name");
String isActive = innerObj.getString("IsActive");
因为它是一个对象数组,所以您必须对 jsonObj 中的所有元素重复上面的操作。
这里有多种选择。这取决于你想要达到的目标:)
快速而肮脏
将其保存为字符串。您必须创建一个新的 class 和 String
对象,然后将您的 JSON 数据放入该对象并保存。
我意识到它不会起作用,因为它不是你真正想要的:)
更好的方法
创建一个包含所有字段的新 class。
// File: ImageData.java
public class ImageData {
private String counter;
private String name;
private String isActiveJpg;
// getters and setters
}
// File: Data.java
public class Data {
private List<ImageData> imageDataList;
// getters and setters
}
准备好这些 class 后,您就可以使用 Jackson or GSON library. Please take a look at the usage example for Jackson here and GSON serialization and deserialization。
旁注
我建议重新考虑设计本身。例如。字段的命名和实体的逻辑非常具有误导性(像 isActiveJpg
这样的东西。它应该是 Set
并且应该有更好的命名方式,比如 isJpg
或类似的东西) .
从我的 UI 通过 ajax 我得到一个 JSON 数据,我正试图将其插入到我的数据库中,我不知道我该怎么做,我知道如何使用 prepared statements
在数据库中插入一些东西,但是在 JSON 的情况下我完全感到困惑
这是我的JSON,我在我的 doPost
{"ImageData":[{"Counter":"Counter A","Name":"CountA1.jpg","IsActive":"Y"},{"Counter":"Counter A","Name":"CountA2.jpg","IsActive":"Y"}]}
servlet 代码 -->
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Scanner scanner = new Scanner(request.getInputStream());
String imageData = scanner.nextLine();
System.out.println(imageData);
}
现在我想像
我需要解析这个吗JSON,我该怎么做ps.setInt(index, 0);
,我不知道
Edit/update
作为@Anjana Senanayake 回答我是这样做的
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Scanner scanner = new Scanner(request.getInputStream());
String imageData = scanner.nextLine();
System.out.println(imageData);
JSONObject jsonObj = new JSONObject(imageData);
JSONArray jsonArray = jsonObj.getJSONArray("ImageData");
JSONObject innerObj = new JSONObject(jsonArray); // this one is throwing error
// eror is `The type of the expression must be an array type but it resolved to JSONArray`
String counter = innerObj.getString("Counter");
String name = innerObj.getString("Name");
String isActive = innerObj.getString("IsActive");
System.out.println(counter);
}
但它在 JSONObject innerObj = new JSONObject(jsonArray[0]);
处抛出错误,错误是 The type of the expression must be an array type but it resolved to JSONArray
使用org.json库解析它并创建JsonObject :-
JSONObject jsonObj = new JSONObject(imageData);
JSONArray jsonArray = jsonObj.getJSONArray("ImageData");
现在,使用此对象获取您的值:-
JSONObject innerObj = new JSONObject(jsonArray[0]);
String counter = innerObj.getString("Counter");
String name = innerObj.getString("Name");
String isActive = innerObj.getString("IsActive");
因为它是一个对象数组,所以您必须对 jsonObj 中的所有元素重复上面的操作。
这里有多种选择。这取决于你想要达到的目标:)
快速而肮脏
将其保存为字符串。您必须创建一个新的 class 和 String
对象,然后将您的 JSON 数据放入该对象并保存。
我意识到它不会起作用,因为它不是你真正想要的:)
更好的方法
创建一个包含所有字段的新 class。
// File: ImageData.java
public class ImageData {
private String counter;
private String name;
private String isActiveJpg;
// getters and setters
}
// File: Data.java
public class Data {
private List<ImageData> imageDataList;
// getters and setters
}
准备好这些 class 后,您就可以使用 Jackson or GSON library. Please take a look at the usage example for Jackson here and GSON serialization and deserialization。
旁注
我建议重新考虑设计本身。例如。字段的命名和实体的逻辑非常具有误导性(像 isActiveJpg
这样的东西。它应该是 Set
并且应该有更好的命名方式,比如 isJpg
或类似的东西) .