【发布时间】:2020-06-22 09:50:31
【问题描述】:
即使对象正确实例化,我也无法读取 json 属性。
首先,我使用 JavaScript 从客户端发送 json:
let object = {
firstName: document.getElementById("firstName").value,
lastName: document.getElementById("lastName").value,
username: document.getElementById("username").value,
password: document.getElementById("password").value,
email: document.getElementById("email").value,
action: "registration"
}
let request = new XMLHttpRequest();
...
在服务器端我有代码:
req.setCharacterEncoding("UTF-8");
JSONObject jsonObject = null;
// String address = "/WEB-INF/pages/login.jsp";
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {
/* report an error */ }
try {
jsonObject = HTTP.toJSONObject(jb.toString());
} catch (JSONException e) {
// crash and burn
throw new IOException("Error parsing JSON request string");
}
String action = jsonObject.getString("firstName");
jsonObject 存在,但程序抛出 org.json.JSONException: JSONObject["firstName"] not found.
【问题讨论】:
-
查看图像(调试器)
Request-URI和Method是您的键 firstName 是键Method的值。如果您想获取 json 结构,请复制该 json 字符串并使用任何在线 json 格式化程序进行解析
标签: javascript java json http web