简体   繁体   English

将JSON字符串反序列化为自定义Java对象

[英]Deserializing a JSON String into a custom Java Object

I have a string that is returned to me that is formatted as a JSON. 我有一个返回给我的字符串,该字符串格式为JSON。 The string looks as follows 字符串如下

{ "Type" : "Notification". "MessageId" : "9343.....". "TopicArn" : "arn-....." "......" }

I have created a custom object that I would like to parse this string into 我创建了一个自定义对象,我想将此字符串解析为

public class AmazonSNSMessage 
{
private String Type;
private String Notification;
private String MessageId;
private String TopicArn;
private String Subject;
private String Message;
private String Timestamp;
private String SignatureVersion;
private String Signature;
private String UnsubscribeURL

// And all the appropriate get/set methods
}

Is there a JSON deserializer in Java that will take the string and create an instance of the AmazonSNSMessage? Java中是否有JSON反序列化器将采用字符串并创建AmazonSNSMessage的实例?

C# Does this by calling this line C#通过调用此行来做到这一点

AmazonSNSMessage b = JsonConvert.DeserializeObject<AmazonSNSMessage>(TheString);

and ideally I would like something similar. 理想情况下,我想要类似的东西。

Jackson can do that: 杰克逊可以做到:

final ObjectMapper mapper = new ObjectMapper();

final AmazonSNSMessage message 
    = mapper.readValue(yourInput, AmazonSNSMessage.class);

It will work automatically since your field names are the same as JSON! 由于您的字段名称与JSON相同,它将自动运行!

If you have more complex scenarios, you can use annotations, custom deserializers etc. 如果您有更复杂的场景,则可以使用注释,自定义解串器等。

I would suggest to use the next library, 我建议使用下一个库,

https://code.google.com/p/json-simple/

I have used it, and it is pretty easy. 我已经用过了,很简单。

http://code.google.com/p/google-gson/ http://code.google.com/p/google-gson/

Gson is a Java library that can be used to convert Java Objects into their JSON representation. Gson是一个Java库,可用于将Java对象转换为其JSON表示形式。 It can also be used to convert a JSON string to an equivalent Java object. 它还可以用于将JSON字符串转换为等效的Java对象。 Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. Gson可以处理任意Java对象,包括您没有源代码的现有对象。

A mode of comment, as fge says, Jackson is a very powerful library to handle Json serialization and deserialization. 正如fge所说,这是一种注释模式,Jackson是一个非常强大的库,用于处理Json序列化和反序列化。 Its very nice map json keys to POJO attributes to get a consistent camel cased code using annotations. 它非常好的映射POJO属性的json键,可以使用注释获得一致的驼峰式代码。

public class AmazonSNSMessage 
{
    @JsonProperty("Type");
    private String type;

    @JsonProperty("Notification");
    private String notification;

    @JsonProperty("MessageId");
    private String messageId;

    @JsonProperty("TopicArn");
    private String topicArn;

    @JsonProperty("Subject");
    private String subject;

    @JsonProperty("Message");
    private String message;

    @JsonProperty("Timestamp");
    private Date timestamp;

    @JsonProperty("SignatureVersion");
    private String signatureVersion;

    @JsonProperty("Signature");
    private String signature;

    @JsonProperty("UnsuscribeUrl");
    private String unsubscribeURL

    // And all the appropriate get/set methods
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM