Java 如何在servlet中读取ajax发送的json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19568142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:24:05  来源:igfitidea点击:

How to read json sent by ajax in servlet

javajqueryajaxjsonservlets

提问by Yosua Sofyan

I'm new to java and I struggling with this problem for 2 days and finally decided to ask here.

我是 Java 新手,我在这个问题上苦苦挣扎了 2 天,最后决定在这里提问。

I am trying to read data sent by jQuery so i can use it in my servlet

我正在尝试读取 jQuery 发送的数据,以便我可以在我的 servlet 中使用它

jQuery

jQuery

var test = [
    {pv: 1000, bv: 2000, mp: 3000, cp: 5000},
    {pv: 2500, bv: 3500, mp: 2000, cp: 4444}
];

$.ajax({
    type: 'post',
    url: 'masterpaket',
    dataType: 'JSON',
    data: 'loadProds=1&'+test, //NB: request.getParameter("loadProds") only return 1, i need to read value of var test
    success: function(data) {

    },
    error: function(data) {
        alert('fail');
    }
});

Servlet

小服务程序

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
   if (request.getParameter("loadProds") != null) {
      //how do i can get the value of pv, bv, mp ,cp
   }
}

I really appreciate any help you can provide.

我非常感谢您能提供的任何帮助。

采纳答案by siledh

You won't be able to parse it on the server unless you send it properly:

除非您正确发送,否则您将无法在服务器上解析它:

$.ajax({
    type: 'get', // it's easier to read GET request parameters
    url: 'masterpaket',
    dataType: 'JSON',
    data: { 
      loadProds: 1,
      test: JSON.stringify(test) // look here!
    },
    success: function(data) {

    },
    error: function(data) {
        alert('fail');
    }
});

You must use JSON.stringifyto send your JavaScript object as JSON string.

您必须使用JSON.stringify将您的 JavaScript 对象作为 JSON 字符串发送。

And then on the server:

然后在服务器上:

String json = request.getParameter("test");

You can parse the jsonstring by hand, or using any library (I would recommend gson).

您可以json手动解析字符串,也可以使用任何库(我推荐gson)。

回答by Rakesh Soni

You will have to use the JSON parser to parse the data into the Servlet

您将不得不使用 JSON 解析器将数据解析到 Servlet

import org.json.simple.JSONObject;


// this parses the json
JSONObject jObj = new JSONObject(request.getParameter("loadProds")); 
Iterator it = jObj.keys(); //gets all the keys

while(it.hasNext())
{
    String key = it.next(); // get key
    Object o = jObj.get(key); // get value
    System.out.println(key + " : " +  o); // print the key and value
}

You will need a json library (e.g Hymanson) to parse the json

您将需要一个 json 库(例如 Hymanson)来解析 json

回答by hlopezvg

Using the import org.json.JSONObject instead of import org.json.simple.JSONObject did the trick for me.

使用 import org.json.JSONObject 而不是 import org.json.simple.JSONObject 对我有用。

See How to create Json object from String containing characters like ':' ,'[' and ']' in Java.

请参阅如何在 Java 中从包含 ':' 、'[' 和 ']' 等字符的 String 创建 Json 对象