Проблема с парсингом JSON файлов
У меня есть класс, с помощью которого я делаю парсинг JSON файла. Вот он:
public class JSONHelper {
public static final String FILE_NAME = Environment.getExternalStorageDirectory() + "/Lalala/data.json";
static File file = new File(FILE_NAME);
public static boolean exportToJSON(Context context, List<TopSong> dataList) {
Gson gson = new Gson();
DataArr dataItems = new DataArr();
dataItems.setSong(dataList);
String jsonString = gson.toJson(dataItems);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream (new File(file.getAbsolutePath().toString()), true); // true will be same as Context.MODE_APPEND
fileOutputStream.write(jsonString.getBytes());
return true;
} catch (Exception e) {
e.printStackTrace();
Log.d("AHHAHAHHAHHA","ERRRRRROOOOOOOORRRRRRRR");
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
public static List<TopSong> importFromJSON(Context context) {
InputStreamReader streamReader = null;
FileInputStream fileInputStream = null;
try{
fileInputStream = new FileInputStream(file);
streamReader = new InputStreamReader(fileInputStream);
Gson gson = new Gson();
DataArr dataItems = gson.fromJson(streamReader, DataArr.class);
return dataItems.getSongs();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
if (streamReader != null) {
try {
streamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static class DataItems {
private TopSong song;
TopSong getSongs() {
return song;
}
void addSong(TopSong song) {
this.song = song;
}
}
public static class DataArr {
public List<TopSong> song;
List<TopSong> getSongs() {
return song;
}
void setSong(List<TopSong> song) {
this.song = song;
}
}
}
Когда я заполняю в json файл данные, то он работает. А вот когда пытаюсь достать их, то выскакивает такая ошибка:
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 176 path $
Вот, как достаю оттуда данные:
List<TopSong> songs = JSONHelper.importFromJSON(getContext());
if(songs != null) {
for (int i = 0; i < songs.size(); i++) {
SongDetail song = new SongDetail(i, "Downloads", songs.get(i).getPerformer(), songs.get(i).getTitle(), songs.get(i).getPath(), 1000);
mSongAdapter.add(song);
}
}