我試圖使用Java Lambda從POST請求中解析「Body」。我遇到了這個錯誤一段時間。json.simple.JSONObject無法使用AWS Lambda轉換爲java.lang.String JAVA
org.json.simple.JSONObject cannot be cast to java.lang.String
但是,當記錄的樣子,身體:
{"body":{"email":"[email protected]"}}
女巫應與解析我'做正確的工作嗎? 奇怪的是插入正在使用JUNIT進行本地化,但是在AWS之後沒有聯機。
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
logger = context.getLogger();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Number user_id = null;
String birthdate = null;
List<Number> company_id = new ArrayList<Number>();
String email = null;
String employment_status = null;
String firstname = null;
String lastname = null;
String login = null;
String profile = null;
List<Number> site_id = new ArrayList<Number>();
String validation_status = null;
JSONObject responseJson = new JSONObject();
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
String filterExpression = "";
String RegionAWS = REGION.toString();
client = AmazonDynamoDBClientBuilder.standard().withRegion(RegionAWS).build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("LI_user");
try {
JSONParser parser = new JSONParser();
JSONObject event = (JSONObject) parser.parse(reader);
logger.log(event.toJSONString());
if (event.get("body") != null) {
JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));
// JSONObject bod = (JSONObject) event.get("body");
if (bod.get("id") != null) {
user_id = (Number)bod.get("id");
}
if (bod.get("birthdate") != null) {
birthdate = (String)bod.get("birthdate");
}
if (bod.get("email") != null) {
email = (String) bod.get("email");
}
if (bod.get("employment_status") != null) {
employment_status = (String) bod.get("employment_status");
}
if (bod.get("firstname") != null) {
firstname = (String) bod.get("firstname");
}
if (bod.get("lastname") != null) {
lastname = (String) bod.get("lastname");
}
if (bod.get("login") != null) {
login = (String) bod.get("login");
}
if (bod.get("profile") != null) {
profile = (String) bod.get("profile");
}
if (bod.get("validation_status") != null) {
validation_status = (String) bod.get("validation_status");
}
}
您已經有了'logger.log(event.toJSONString());'爲'event'是'JSONObject' 。 (JSONObject)parser.parse((String)event.get(「body」));' with 'JSONObject bod =(JSONObject)event.get(「body」);}爲什麼不更換'JSONObject bod = ' –