我正在使用servlet接收JSON,如下所示:
try {
JSONObject jsonObject = HTTP.toJSONObject(jb.toString());
System.out.println("JSON STRING RECEIVED**************** " + jb.toString());//DEBUG LINE
String TransID = jsonObject.getString("TransID");
String TransTime = jsonObject.getString("TransTime");
String TransAmount = jsonObject.getString("TransAmount");
String BusinessShortCode = jsonObject.getString("BusinessShortCode");
String BillRefNumber = jsonObject.getString("BillRefNumber");
String MSISDN = jsonObject.getString("MSISDN");
String FirstName = jsonObject.getString("FirstName");
String MiddleName = jsonObject.getString("MiddleName");
String LastName = jsonObject.getString("LastName");
int notificationId = Notification.addRawNotitifcation(TransID, TransTime, Double.parseDouble(TransAmount), BusinessShortCode, BillRefNumber, MSISDN, FirstName, MiddleName, LastName, new Date(), "N");
if (notificationId > 0) {
log.debug("Message Transaction Id " + TransID + " Received SuccessFully");
} else {
log.debug("Error Processing Transaction Id " + TransID);
}
response.setContentType("application/json");
} catch (JSONException asd) {
// crash and burn
log.debug(asd.getMessage());
throw new IOException("Error parsing JSON request string");
}
}
在DEBUG LINE上我收到以下JSON
{
"TransactionType":"",
"TransID":"MDE51H4CZB",
"TransTime":"20180414104546",
"TransAmount":"100.00",
"BusinessShortCode":"600534",
"BillRefNumber":"1131700102",
"InvoiceNumber":"",
"OrgAccountBalance":"",
"ThirdPartyTransID":"",
"MSISDN":"254708374149",
"FirstName":"John",
"MiddleName":"J.",
"LastName":"Doe"
}
修改
System.out.println(jsonObject.toString());
返回:
{
"Request-URI":"TransactionType",
"Method":"{",
"HTTP-Version":":",
"\"\", \"TransID\"":"\"MDE71H4CZD\", \"TransTime\": \"20180414115011\", \"TransAmount\": \"100.00\", \"BusinessShortCode\": \"600534\", \"BillRefNumber\": \"1131700102\", \"InvoiceNumber\": \"\", \"OrgAccountBalance\": \"\", \"ThirdPartyTransID\": \"\", \"MSISDN\": \"254708374149\", \"FirstName\": \"John\", \"MiddleName\": \"J.\", \"LastName\": \"Doe\" }"
}
但是我找不到错误JSONObject ["TransID"]
。我错过了什么?