user3449953
user3449953

Reputation: 53

JSON to object in Java

I have the following JSON:

{
       "book":{
             "isbn" : "12356789",
             "title" : "Algorithm",
             "author" : [
                           "Cormen",
                           "Rivest",
                           "Stein"
             ],
             "price" : 45.78
       }
}

I need to convert this JSON string to a Book class. I don't want to set it property by property. Also, I don't want to use Gson.

I want to do something as:

Book book=jsonReader.readObject().toClass(Book.class);

How can I do it with javax.json.Json or Moxy?

Upvotes: 0

Views: 497

Answers (3)

user3449953
user3449953

Reputation: 53

Thanks for your answers. If I use Jackson, I will have to add another library to my project (something that I don't want to do) I found the answer to my problem here http://blog.bdoughan.com/2013/07/eclipselink-moxy-and-java-api-for-json.html

Upvotes: 0

Hector
Hector

Reputation: 5356

I would recommend NOT using anything other than Jackson to process json.

A Jackson based solution for the JSON you have pasted would resemble this:-

First create a POJO for your book object

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class BookVO {

    private final String isbn;
    private final String title;
    private final String[] author;
    private final double price;

    @JsonCreator
    public BookVO(@JsonProperty("isbn") final String isbn, @JsonProperty("title") final String title, @JsonProperty("author") final String[] author, @JsonProperty("price") final double price) {
        super();
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public String[] getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

}

You then need a Book container POJO

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Books {

    private final BookVO book;

    @JsonCreator
    public Books(@JsonProperty("book") final BookVO book) {
        super();
        this.book = book;
    }

    public BookVO getBook() {
        return book;
    }
}

Finally you need to convert JSON to Java Objects as follows:-

public static void main(final String[] args) throws JsonParseException, JsonMappingException, IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final Books books = mapper.readValue(new File("book.json"), Books.class);

    System.out.println(books);

}

The content of book.json is

{
       "book":{
             "isbn" : "12356789",
             "title" : "Algorithm",
             "author" : [
                           "Cormen",
                           "Rivest",
                           "Stein"
             ],
             "price" : 45.78
       }
}

Upvotes: 1

Farhad
Farhad

Reputation: 388

I've used Jackson parser to do this. Take a look at the ObjectMapper class.

public static <T> Object getObjectFromJsonString(String json,
        Class<T> className) throws JsonParseException,
        JsonMappingException, IOException {
    InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
    return objectMapper.readValue(is, className);
}

Upvotes: 0

Related Questions