gpt4 book ai didi

java - 使用 GSON 解析整个 AWS SQS 记录

转载 作者:行者123 更新时间:2023-11-30 02:01:49 26 4
gpt4 key购买 nike

我现在一直尝试解析此消息,但没有成功。使用 AWS Simple Queue Service API,我按照说明执行以下操作...

for(Message m : Messages){
System.out.println(m.getBody());
}

这将返回以下结构中的 JSON 字符串:

{
"Records": [
{
"EventSource": "",
"EventVersion": "",
"EventSubscriptionArn": "",
"Sns": {
"Type": "",
"MessageId": "",
"TopicArn": "",
"Subject": null,
"Message": ""
"Timestamp": "",
"SignatureVersion": "",
"Signature": "”
"SigningCertUrl": "",
"UnsubscribeUrl": "",
"MessageAttributes": {}
}
}
]
}

我一直在尝试使用 GSON 将整个事情解析为 Java 对象,以便我可以提取“Message”参数(也包含 JSON),然后使用 GSON 解析它(当我传递该参数时完成并工作)直接发短信)。

这些是我设置的类,但这行不通——每个类都有公共(public) getter 和 setter......

记录类别:

public class Records {

public ArrayList<ExceptionMessages> exceptionMessages = new ArrayList<ExceptionMessages>();

public ArrayList<ExceptionMessages> getExceptionMessages() {
return exceptionMessages;
}

public void setExceptionMessages(ArrayList<ExceptionMessages> exceptionMessages) {
this.exceptionMessages = exceptionMessages;
}

消息类别:

public class ExceptionMessages {

public String EventSource;
public String EventVersion;
public String EventSubscriptionArn;
public Sns messageJSON;
}

Sns 类(存储消息的位置):

public class Sns {

public String Type;
public String MessageId;
public String TopicArn;
public String Subject;
public String Message;
public String Timestamp;
public String SignatureVersion;
public String Signature;
public String SigningCertUrl;
public String UnsubscribeUrl;
public String MessageAttributes;
}

尝试 ArrayList 的 .get(0) 时出现空指针异常,因此它是空的并且未进行解析。

这就是我的称呼...

我将 m.getBody() 发送到解析方法并尝试像这样解析:

Gson gson = new Gson();
Records record = new Records();
gson.fromJson(JSONString.replaceAll("\\s+", ""), Records.class);

最佳答案

结构应该是

class RecordContainer {
ArrayList<Record> Records;
}

class Record {
public String EventSource;
public String EventVersion;
public String EventSubscriptionArn;
public Sns Sns;
}

class Sns {
public String Type;
public String MessageId;
public String TopicArn;
public String Subject;
public String Message;
public String Timestamp;
public String SignatureVersion;
public String Signature;
public String SigningCertUrl;
public String UnsubscribeUrl;
public MessageAttributes MessageAttributes;
}

关于java - 使用 GSON 解析整个 AWS SQS 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52592429/

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