Below error while reading JSON from file
"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"
Below is my code
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) {}
}
}
}
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 + "]";
}
}
JSON:
[{
"name": "The King",
"isbn": 67786,
"aisle": "hfhdh",
"author": "Biju"
},
{
"name": "The King",
"isbn": 67786,
"aisle": "hfhdh",
"author": "Biju"
}
]
A small change in code, Instead of
List<T> arraylist = gson.fromJson(filepath, ListType);
Use
List<T> arraylist = gson.fromJson(bufferReader, ListType);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.