簡體   English   中英

將字符串轉換為jsonobject時發生異常

[英]exception while convert string to jsonobject

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM