这是表单数据
selectedDtlId: [{"id":"3","isReviewed":true,"notes":"notes asdf test add 2"},{"id":"2","isReviewed":true,"notes":""},{"id":"1","isReviewed":true,"notes":""}]
isReviewedAll: true
notesAll:
在控制器中运行以下代码后
Gson gson = new Gson();
gson.toJson(request.getParameter("selectedDtlId"));
响应
"[{\"id\":\"3\",\"isReviewed\":true,\"notes\":\"notes asdf test add 2\"},{\"id\":\"2\",\"isReviewed\":true,\"notes\":\"\"},{\"id\":\"1\",\"isReviewed\":true,\"notes\":\"\"}]"
预期响应
[
{
"id": "3",
"isReviewed": true,
"notes": "notes asdf test add 2"
},
{
"id": "2",
"isReviewed": true,
"notes": ""
},
{
"id": "1",
"isReviewed": true,
"notes": ""
}
]
答案 0 :(得分:4)
您应该从Json打电话给Json:
gson.fromJson(request.getParameter("selectedDtlId"), JsonElement.class);
或者,如果您有模型:
gson.fromJson(request.getParameter("selectedDtlId"), MyModel.class);
答案 1 :(得分:2)
您应将其映射到类。 (另外,当您应该从Json打电话给您时,您也在向Json打电话)
主要
session = HTMLSession()
req = session.get(url)
# do something
req.close()
session.close()
模型
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
String jsonInString = "[{\"id\":\"3\",\"isReviewed\":true,\"notes\":\"notes asdf test add 2\"},{\"id\":\"2\",\"isReviewed\":true,\"notes\":\"\"},{\"id\":\"1\",\"isReviewed\":true,\"notes\":\"\"}]";
Gson gson = new Gson();
SelectedDtlId[] selectedDtlIds = gson.fromJson(jsonInString, SelectedDtlId[].class);
for (SelectedDtlId selectedDtlId : selectedDtlIds) {
System.out.println("id: " + selectedDtlId.getId());
System.out.println("notes: " + selectedDtlId.getNotes());
System.out.println("isReviewed: " + selectedDtlId.isReviewed());
}
}
}
结果应该是:
import com.google.gson.annotations.SerializedName;
public class SelectedDtlId {
@SerializedName("id")
private int id;
@SerializedName("isReviewed")
private boolean isReviewed;
@SerializedName("notes")
private String notes;
// getters & setters
}