gpt4 book ai didi

java - 将字符串转换为 jsonobject 时出现异常

转载 作者:行者123 更新时间:2023-12-01 22:37:53 24 4
gpt4 key购买 nike

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

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

其中 bookName 是一个数组。我想访问数组中的值并填充到 bean 中。但是,当我尝试将字符串转换为 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);

我正在做的是将字符串转换为映射,然后将其添加到 JSONObject。

因为有很多 json 序列化器,但不确定哪个是最好的。现在,我有 net.sf.json.JSONObjectcom.google.gson.JsonObject

有人可以帮我解决这个问题吗?

提前致谢

最佳答案

您可以将 JSON 映射到 POJO。
如果书除了名称之外还有更多属性,您将需要两个 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;
}
}

您的 JSON 将如下所示:

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

然后你可以使用 Gson 来解析你的 JSON:

Gson gson = new Gson();
Shelf shelf = gson.fromJson(inputData, Shelf.class);
<小时/>

更新

考虑到您的 JSON 看起来像这样(这本书可以表示为 String):

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

只需一个带有 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;
}
}

关于java - 将字符串转换为 jsonobject 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26627400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com