gpt4 book ai didi

java - json.simple.JSONObject 无法使用 AWS Lambda JAVA 转换为 java.lang.String

转载 作者:行者123 更新时间:2023-12-02 12:09:01 25 4
gpt4 key购买 nike

我正在尝试使用 Java Lambda 解析 POST 请求中的“正文”。我被这个错误困扰了一段时间。

 org.json.simple.JSONObject cannot be cast to java.lang.String

但是登录时的正文看起来像这样:

{"body":{"email":"test@test.com"}}

女巫应该与我正在做的解析一起工作,对吗?奇怪的是,插入在本地使用 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");
}
}

最佳答案

替换

JSONObject bod = (JSONObject)parser.parse((String)event.get("body"));

JSONObject bod = (JSONObject)event.get("body");

if event 始终是 JSONObject 的实例(这似乎是理所当然的,否则您将在 JSONObject event = (JSONObject) parser.parse 处获得 ClassCast (读者);)

您看到已记录

{"body":{"email":"test@test.com"}}

只是因为logger.log(event.toJSONString());

关于java - json.simple.JSONObject 无法使用 AWS Lambda JAVA 转换为 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46685197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com