简体   繁体   中英

exception while convert string to jsonobject

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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