簡體   English   中英

無法從文件中讀取 JSON

[英]Unable To Read JSON From File

從文件中讀取 JSON 時出現以下錯誤

"Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
 at line 1 column 1 path $
        at com.google.gson.Gson.fromJson(Gson.java:826)
        at com.google.gson.Gson.fromJson(Gson.java:779)
        at com.google.gson.Gson.fromJson(Gson.java:728)
        at resources.ReadJsonFile.getjsondata(ReadJsonFile.java:50)
        at resources.ReadJsonFile.main(ReadJsonFile.java:30)
     Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
        at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
        at com.google.gson.Gson.fromJson(Gson.java:814)
        ... 4 more"

下面是我的代碼

public class ReadJsonFile < T > {

    public static void main(String[] args) {
        ReadJsonFile read = new ReadJsonFile();
        read.getjsondata("src/test/resource/testdataresource/addplacerequest.json",
            Addbook.class);
    }
    public List < T > getjsondata(String filepath, Class < T > typeofT) {

        //filepath= "src/test/resource/testdataresource/addplacerequest.json";
        Gson gson = new Gson();;
        BufferedReader bufferReader = null;
        try {

            bufferReader = new BufferedReader(new FileReader(filepath));

            Type ListType = new TypeToken < ArrayList < T >> () {}.getType();
            /
            List < T > arraylist = gson.fromJson(filepath, ListType);
            Iterator iter = arraylist.iterator();
            while (iter.hasNext()) {
                System.out.println(iter.next());
            }
            return (arraylist);

        } catch (FileNotFoundException e) {
            throw new RuntimeException("Json file not found at path : " + filepath);
        } finally {
            try {
                if (bufferReader != null) bufferReader.close();
            } catch (IOException ignore) {}
        }
    }



}

寶卓 class:

public class Addbook {


    public String name;
    public int isbn;
    public String aisle;
    public String author;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getIsbn() {
        return isbn;
    }
    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public String getAisle() {
        return aisle;
    }
    public void setAisle(String aisle) {
        this.aisle = aisle;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Addbook [name=" + name + ",isbn=" + isbn + ",aisle=" + aisle + ",authur=" + author + "]";
    }




}

JSON:

[{
        "name": "The King",
        "isbn": 67786,
        "aisle": "hfhdh",
        "author": "Biju"
    },
    {
        "name": "The King",
        "isbn": 67786,
        "aisle": "hfhdh",
        "author": "Biju"
    }
]

代碼中的一個小改動,而不是

List<T> arraylist = gson.fromJson(filepath, ListType); 

利用

List<T> arraylist = gson.fromJson(bufferReader, ListType);


暫無
暫無

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

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