无法读取json文件对象

问题描述 投票:0回答:1

读取json文件的文件读取错误以下

"Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING

在第1行第1列路径$在com.google.gson.Gson.fromJson(Gson.java:826)在com.google.gson.Gson.fromJson(Gson.java:779)在com.google.gson.Gson.fromJson(Gson.java:728)在resources.ReadJsonFile.getjsondata(ReadJsonFile.java:50)在resources.ReadJsonFile.main(ReadJsonFile.java:30)由以下原因引起:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第1列路径$ STRING在com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:79)在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:60)在com.google.gson.Gson.fromJson(Gson.java:814)...另外4个“

Below are the codes used to read the json file


    public class ReadJsonFile<T>  {

      public static void main (String[] args)
      {
          ReadJsonFile read  = new ReadJsonFile();
          read.getjsondata("src/test/resource/testdataresource/addplacerequest.json",

Addbook.class);}公共列表getjsondata(String filepath,Class 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) {}
    }
      }



    }



    Pojo 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 +"]";
        }




    }


    Jsonfile:

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

>

java gson rest-assured
1个回答
0
投票
List<T> arraylist = gson.fromJson(bufferReader, ListType);
© www.soinside.com 2019 - 2024. All rights reserved.