简体   繁体   English

将字符串转换为jsonobject时发生异常

[英]exception while convert string to jsonobject

I have a String in my servlet which is of the following format. 我的servlet中有一个字符串,格式如下。

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

where the bookName is an array. 其中bookName是一个数组。 I want to access the values in the array and populate in the bean. 我想访问数组中的值并在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时,出现以下异常,因为bookName是一个数组com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY这就是我要尝试的方式做吧

     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. 我正在做的是,将字符串转换为映射,然后将其添加到JSONObject。

Since there are many json serializers and not sure which is the best. 由于存在许多json序列化程序,因此不确定哪个是最好的。 Right now, I have net.sf.json.JSONObject and com.google.gson.JsonObject 现在,我有net.sf.json.JSONObjectcom.google.gson.JsonObject

Can someone help me to get this solved. 有人可以帮我解决这个问题。

Thanks in advance 提前致谢

You can map your JSON to a POJO. 您可以将JSON映射到POJO。
If the book will have more attributes besides the name, you'll need two POJOs, as you can see below. 如果这本书除了名称之外还将具有更多属性,则您将需要两个POJO,如下所示。

A POJO for the book: 这本书的POJO:

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: 还有一个书架的POJO,其中包含书籍清单:

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: 您的JSON将如下所示:

{
    "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解析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 ): 考虑到您的JSON看起来像这样(这本书可以表示为String ):

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

Only one POJO with a list of String is enough: 仅一个带有String列表的POJO就足够了:

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

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

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