将JSON从Ajax请求转换为Java中的JSON对象

将JSON从Ajax请求转换为Java中的JSON对象

这是表格数据

selectedDtlId: [{"id":"3","isReviewed":true,"notes":"notes asdf test add 2"},{"id":"2","isReviewed":true,"notes":""},{"id":"1","isReviewed":true,"notes":""}]
isReviewedAll: true
notesAll:



在Controller中运行以下代码后

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": ""
   }
]

最佳答案

当您应该从Json呼叫时,您正在呼叫toJson:

gson.fromJson(request.getParameter("selectedDtlId"), JsonElement.class);


或者,如果您有模型:

gson.fromJson(request.getParameter("selectedDtlId"), MyModel.class);

关于java - 将JSON从Ajax请求转换为Java中的JSON对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51271482/

10-09 02:08