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

Java 将字符串转换为jsonobject时发生异常,java,json,Java,Json,我的servlet中有一个字符串,其格式如下 { "name": "Jam", "noOfBooksRequired": "2", "type": "Type 1", "bookName": [ "The Magic", "The Power" ] } 其中,bookName是一个数组。我想访问数组中的值并填充到bean中。但是,当我尝试将字符串转换为jso

我的servlet中有一个字符串,其格式如下

 {
        "name": "Jam",
        "noOfBooksRequired": "2",
        "type": "Type 1",
        "bookName": [
            "The Magic",
            "The Power"
        ]
    }
其中,
bookName
是一个数组。我想访问数组中的值并填充到bean中。但是,当我尝试将字符串转换为jsonobject时,我得到了以下异常,因为bookName是一个数组
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应该是一个字符串,但却是BEGIN\u 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);
JSONObject js=new JSONObject();
字符串inputData=request.getParameter(“inputData”);
HashMap hmap=新的HashMap();
Type Type=new-TypeToken(){}.getType();
hmap=gson.fromJson(输入数据,类型);
js.putAll(hmap);
我要做的是,将字符串转换为映射,然后将其添加到JSONObject

因为有很多json序列化程序,不确定哪一个是最好的。现在,我有
net.sf.json.JSONObject
com.google.gson.JSONObject

有人能帮我解决这个问题吗


提前感谢

您可以将JSON映射到POJO。
如果这本书除了名称之外还有更多属性,那么您将需要两个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;
    }
}
书架上还有一个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;
    }
}
然后您可以使用Gson解析JSON:

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

更新 考虑到您的JSON如下所示(本书可以表示为
字符串
):

只有一个包含
字符串列表的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;
    }
}
类架{
私有字符串名称;
私有整数noOfBooksRequired;
私有字符串类型;
私人书目;
公共货架(){
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public Integer getNoOfBooksRequired(){
返回noOfBooksRequired;
}
public void setNoOfBooksRequired(整数noOfBooksRequired){
this.noOfBooksRequired=noOfBooksRequired;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共列表getBooks(){
还书;
}
公共书籍(列表书籍){
这本书=书;
}
}

您可以将JSON映射到POJO。
如果这本书除了名称之外还有更多属性,那么您将需要两个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;
    }
}
书架上还有一个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;
    }
}
然后您可以使用Gson解析JSON:

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

更新 考虑到您的JSON如下所示(本书可以表示为
字符串
):

只有一个包含
字符串列表的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;
    }
}
类架{
私有字符串名称;
私有整数noOfBooksRequired;
私有字符串类型;
私人书目;
公共货架(){
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public Integer getNoOfBooksRequired(){
返回noOfBooksRequired;
}
public void setNoOfBooksRequired(整数noOfBooksRequired){
this.noOfBooksRequired=noOfBooksRequired;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共列表getBooks(){
还书;
}
公共书籍(列表书籍){
这本书=书;
}
}

您可以将JSON映射到POJO。
如果这本书除了名称之外还有更多属性,那么您将需要两个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;
    }
}
书架上还有一个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;
    }
}
然后您可以使用Gson解析JSON:

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

更新 考虑到您的JSON如下所示(本书可以表示为
字符串
):

只有一个包含
字符串列表的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;
    }
}
类架{
私有字符串名称;
私有整数noOfBooksRequired;
私有字符串类型;
私人书目;
公共货架(){
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public Integer getNoOfBooksRequired(){
返回noOfBooksRequired;
}
public void setNoOfBooksRequired(整数noOfBooksRequired){
this.noOfBooksRequired=noOfBooksRequired;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共列表getBooks(){
还书;
}
公共书籍(列表书籍){
这本书=书;
}
}

您可以将JSON映射到POJO。
如果这本书除了名称之外还有更多属性,那么您将需要两个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;
    }
}
书架上还有一个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;
    }
}
然后您可以使用Gson解析JSON:

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

更新 考虑到您的JSON如下所示(本书可以表示为
字符串
):

只有一个包含
字符串列表的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;
    }
}
类架{
私有字符串名称;
私有整数noOfBooksRequired;
私有字符串类型;
私人书目;
公共货架(){
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public Integer getNoOfBooksRequired(){
返回noOfBooksRequired;
}
public void setNoOfBooksRequired(整数noOfBooksRequired){
this.noOfBooksRequired=noOfBooksRequired;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共列表getBooks(){
还书;
}
公共书籍(列表书籍){
这是我的书=