0

I have a string that is returned to me that is formatted as a 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?

C# Does this by calling this line

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

and ideally I would like something similar.

0

4 Answers 4

2

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!

If you have more complex scenarios, you can use annotations, custom deserializers etc.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Comments

0

I would suggest to use the next library,

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

I have used it, and it is pretty easy.

Comments

0

A mode of comment, as fge says, Jackson is a very powerful library to handle Json serialization and deserialization. Its very nice map json keys to POJO attributes to get a consistent camel cased code using annotations.

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.