Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GSON将Java复杂对象转换为JSON_Java_Android_Json_Gson - Fatal编程技术网

使用GSON将Java复杂对象转换为JSON

使用GSON将Java复杂对象转换为JSON,java,android,json,gson,Java,Android,Json,Gson,我需要在MyBook中列出一些对象的子列表,并将其保存到android内部存储器中。每个子列表都有一个名称 我的书: public class MyBook implements Parcelable{ String name; List<String> authors; String publisher; Bitmap preview; String description; List<String> isbn; List<String> isbnType

我需要在MyBook中列出一些对象的子列表,并将其保存到android内部存储器中。每个子列表都有一个名称

我的书:

public class MyBook implements Parcelable{

String name;
List<String> authors;
String publisher;
Bitmap preview;
String description;
List<String> isbn;
List<String> isbnType;
//String isbn;
String imgLink="";
String googleLink ="";
String publishedDate;
MyBook子列表列表:

public class ListOfMyBook extends ArrayList<MyBook>{
    public String listName;

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return listName;
    }

}
我当前用于序列化和反序列化的代码:

public static final String MY_LIST_FILE = "myList.json";
public static void exportToXml(Context context, List<ListOfMyBook> listListBook){
    Gson gson = new Gson();
    String json = gson.toJson(listListBook);
    Log.d("GSON",json);
    FileOutputStream outputStream;

    try {
      outputStream = context.openFileOutput(MY_LIST_FILE, Context.MODE_PRIVATE);
      outputStream.write(json.getBytes());
      outputStream.close();
    } catch (Exception e) {
        Log.d("exportToXml",e.toString());
      e.printStackTrace();
    }
}


public static List<ListOfMyBook> importFromXml(Context context){
    FileInputStream fis = null;
    try {
        fis = context.openFileInput(MY_LIST_FILE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     InputStreamReader isr = new InputStreamReader(fis);
     BufferedReader bufferedReader = new BufferedReader(isr);
     StringBuilder sb = new StringBuilder();
     String line;
     try {
        while ((line = bufferedReader.readLine()) != null) {
             sb.append(line);
         }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     String json = sb.toString();

    Type listType = new TypeToken<List<ListOfMyBook>>() {}.getType();
    Gson gson = new Gson();
    return gson.fromJson(json, listType);
}

这不会保存ListOfMyBook的名称,也不会保存空的ListOfMyBook。有更好的实施方案吗?类可以修改。

因此,我实际上提出了自己的解决方案,即不扩展ArrayList,而是创建一个新类,如下所示:

public class ListOfMyBook implements Parcelable{
    public String listName;
    public List<MyBook> listOfBook = new ArrayList<MyBook>();
}