简体   繁体   English

Java从HttpServletRequest获取JSON数据

[英]Java get JSON data from HttpServletRequest

I want to get Json key values from HttpServletRequest. 我想从HttpServletRequest获取Json键值。

My Java code is given below 我的Java代码如下

import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

@WebServlet("/Service")
public class Service extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, JSONException {
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
        } catch (Exception e) {
        }

        try {
            JSONObject jsonObject = HTTP.toJSONObject(jb.toString());
            String email = jsonObject.getString("email");
        } catch (Exception e) {
        }
    }
}

Post JSON 发布JSON

{
    "email": "test@xyz.com",
    "fname": "test01"
}

I am getting below JSON output using JSONObject jsonObject = HTTP.toJSONObject(jb.toString()); 我正在使用JSONObject jsonObject = HTTP.toJSONObject(jb.toString());低于JSON输出JSONObject jsonObject = HTTP.toJSONObject(jb.toString());

{"\"test@xyz.com\",\t\"fname\"":"\"test01\"}","Request-URI":"email","Method":"{","HTTP-Version":":"}

I am not getting any value using String email = jsonObject.getString("email"); 我没有使用String email = jsonObject.getString("email");获得任何值String email = jsonObject.getString("email");

I am using Eclipse Mars 1 using JAVA. 我正在使用JAVA使用Eclipse Mars 1。

How are you passing your json in? 您如何传递json?

If you are passing it in as Content-Type: application/x-www-form-urlencoded , then the above won't work - you should use something like 如果您以Content-Type: application/x-www-form-urlencoded传递它,则以上内容将无法正常工作-您应使用类似

JSONObject jObj = new JSONObject(request.getParameter("mydata"));

where mydata is the name of the HTML form field. 其中mydata是HTML表单字段的名称。

If you use Content-Type: application/json , the code you provided should work. 如果您使用Content-Type: application/json ,则您提供的代码应该可以使用。 Can you confirm that? 你能确认吗?

It's worth anyway to output the contents of jb.toString() in the servlet, so you can see what came into it before trying to parse it as JSON if it's invalid already. 无论如何,在servlet中输出jb.toString()的内容都是jb.toString()的,因此,如果它已经无效,则可以在尝试将其解析为JSON之前先查看其中的内容。

If you can use curl, test your code with: 如果可以使用curl,请使用以下命令测试代码:

curl http://localhost:8080/ -H "Content-Type: application/json" -X POST -d '{"email": "test@xyz.com","fname": "test01"}'

or the code such as in the last reference below. 或以下最后参考中的代码。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM