0

I have a String in my servlet which is of the following format.

 {
        "name": "Jam",
        "noOfBooksRequired": "2",
        "type": "Type 1",
        "bookName": [
            "The Magic",
            "The Power"
        ]
    }

where the bookName is an array. I want to access the values in the array and populate in the bean. But, when I try to convert the string to jsonobject, I am getting the following exception because bookName is an array com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY This is how I am trying to do it

     JSONObject js= new JSONObject();
     String inputData= request.getParameter("inputData");
     HashMap<String, String> hmap= new HashMap<String, String>();


     Type type = new TypeToken<HashMap<String, String>>(){}.getType();
     hmap = gson.fromJson(inputData, type);
     js.putAll(hmap);

What I am doing is, I convert the string to a map and then add it to the JSONObject.

Since there are many json serializers and not sure which is the best. Right now, I have net.sf.json.JSONObject and com.google.gson.JsonObject

Can someone help me to get this solved.

Thanks in advance

7
  • How do you expect a JSON array to be deserialized as a String (member value of bookName? Why don't you create a POJO to map to? Commented Oct 29, 2014 at 10:17
  • The name,noOfBooks,type will be mapped to a POJO, i want to create a different POJO for Book(the bookName)..actually,there are few more attributes to Book. So, I want to access the values in that array.. But not sure how to do that. Commented Oct 29, 2014 at 10:23
  • 1
    Try changing the TypeToken map from HashMap<String, String> to HashMap<String, Object>. Commented Oct 29, 2014 at 10:24
  • @Shruthi, I have edited my answer. Please, take a look. Commented Oct 29, 2014 at 10:43
  • @cassio Thanks..I will try that in my code :) Commented Oct 29, 2014 at 10:57

1 Answer 1

3

You can map your JSON to a POJO.
If the book will have more attributes besides the name, you'll need two POJOs, as you can see below.

A POJO for the book:

class Book {

    private String name;
    private String author;

    public Book() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

And a POJO for the shelf, which have a list of books:

class Shelf {

    private String name;
    private Integer noOfBooksRequired;
    private String type;
    private List<Book> books;

    public Shelf() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getNoOfBooksRequired() {
        return noOfBooksRequired;
    }

    public void setNoOfBooksRequired(Integer noOfBooksRequired) {
        this.noOfBooksRequired = noOfBooksRequired;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }
}

Your JSON will look like this:

{
    "name": "Jam",
    "noOfBooksRequired": "2",
    "type": "Type 1",
    "books": [
        {"name": "The Magic", "author": "John Doe"},
        {"name": "The Power", "author": "Jane Doe"}
    ]
}

And then you can use Gson to parse your JSON:

Gson gson = new Gson();
Shelf shelf = gson.fromJson(inputData, Shelf.class);

Update

Considering your JSON looks like this (the book can be represented as a String):

{
    "name": "Jam",
    "noOfBooksRequired": "2",
    "type": "Type 1",
    "books": [
        "The Magic",
        "The Power"
    ]
}

Only one POJO with a list of String is enough:

class Shelf {

    private String name;
    private Integer noOfBooksRequired;
    private String type;
    private List<String> books;

    public Shelf() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getNoOfBooksRequired() {
        return noOfBooksRequired;
    }

    public void setNoOfBooksRequired(Integer noOfBooksRequired) {
        this.noOfBooksRequired = noOfBooksRequired;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<String> getBooks() {
        return books;
    }

    public void setBooks(List<String> books) {
        this.books = books;
    }
}
Sign up to request clarification or add additional context in comments.

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.