因此,我打算将输入的数据存储到JSON文件中,并且希望将该JSON文件存储到手机的本地存储中。但是我的代码中有这个错误。关于JsonWriter。谁能帮我?
// THIS IS MY CODE
private void writeToFile(OutputStream out, List<Wordbank> wordList) throws IOException{
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndent(" ");
writeWordbankArray(writer, wordList);
writer.close();
}
public void writeWordbankArray(JsonWriter writer, List<Wordbank> wordList) throws IOException{
writer.beginArray();
for(Wordbank wl : wordList){
writeWordbank(writer, wl);
}
writer.endArray();
}
public void writeWordbank(JsonWriter writer, Wordbank wordbank) throws IOException{
writer.beginObject();
writer.name("English").value(wordbank.getEnglish());
writer.name("Cebuano").value(wordbank.getCebuano());
writer.name("Pronounciation").value(wordbank.getPronounciation());
writer.name("POS").value(wordbank.getPos());
writer.name("Audio").value(wordbank.getAudio());
if(wordbank.getImage() != null || wordbank.getFx() != null){
writer.name("Picture").value(wordbank.getImage());
writer.name("Effect").value(wordbank.getFx());
}else{
writer.name("No name").nullValue();
}
writer.endObject();
}
//下面是错误
2018-10-18 18:48:52.372 4965-4965/com.example.sampleFile/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sampleFile, PID: 4965
java.lang.NullPointerException
at java.io.Writer.<init>(Writer.java:88)
at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:97)
at com.example.sampleFile.backend.AddData.writeToFile(AddData.java:518)
at com.example.sampleFile.backend.AddData.access$500(AddData.java:61)
at com.example.sampleFile.backend.AddData$15.onSuccess(AddData.java:460)
at com.example.sampleFile.backend.AddData$15.onSuccess(AddData.java:453)
at com.google.firebase.storage.StorageTask.zza(com.google.firebase:firebase-storage@@16.0.2:123)
at com.google.firebase.storage.zze.zza(Unknown Source:6)
at com.google.firebase.storage.zzv.zza(com.google.firebase:firebase-storage@@16.0.2:89)
at com.google.firebase.storage.zzy.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
以上是错误。我需要帮助,请帮助我!
答案 0 :(得分:0)
try {
File file = new File("<storage location>" + filename + ".json");
Writer writer = new BufferedWriter(new FileWriter(file));
writer.write(jsonObject.toString());
writer.close();
} catch (Exception e) {
Log.e("File Exception: ", e.getMessage());
}
答案 1 :(得分:0)
您可以使用另一种方法。 以下代码
writer.name("English").value(wordbank.getEnglish());
writer.name("Cebuano").value(wordbank.getCebuano());
writer.name("Pronounciation").value(wordbank.getPronounciation());
writer.name("POS").value(wordbank.getPos());
writer.name("Audio").value(wordbank.getAudio());
if(wordbank.getImage() != null || wordbank.getFx() != null){
writer.name("Picture").value(wordbank.getImage());
writer.name("Effect").value(wordbank.getFx());
}else{
writer.name("No name").nullValue();
}
告诉我您已经知道了什么,可以创建一个POJO类并使用Gson库将该POJO对象解析为JSON格式的字符串。以后可以用作
JSONObject jsonObj = new JSONObject(Jsonstring);
这是一个例子
ModelPOJO modelPojo = new ModelPojo();
modelPojo.setEnglish("value");
Gson gson = new Gson();
String jsonString = gson.toJson(modelPojo);
jsonString是必需的字符串。 要将其写入本地存储,可以采用@jake breen的方法。
希望有帮助! :)