我想通过这个函数写入文件,但总是返回一个异常NESTING PROBLEM。我的错误在哪里?异常出现在这一行代码中:
writer.name("code").value(s);
请帮我
这是电话:
userA.Write(file_name, getActivity().getBaseContext(),Prodotto);
public static void Write(String fileName,Context context,String s) {
JsonWriter writer;
File myDir, file_1;
String file;
try {
myDir = context.getFilesDir();
file = myDir.getAbsolutePath().toString();
file_1 = context.getFileStreamPath(fileName);
if(!file_1.exists()) {
//not exist file, create it
file_1 = new File(myDir, fileName);
}
//writer = new JsonWriter(new FileWriter(file+ "/"+ fileName));
writer = new JsonWriter(new FileWriter(file+ "/"+ fileName));
writer.beginObject(); // {
writer.name("items"); // "items"
writer.beginArray(); // [
//ciclo for per tutti gli oggetti presenti
writer.name("code").value(s); // "code" : 29
writer.name("type").value(s); // "type"
writer.name("name").value(s); // "name"
writer.name("size").value(s); // "size"
writer.endArray(); // ]
writer.endObject(); // }
writer.close();
System.out.println("Write - Done");
} catch (IOException e) {
e.printStackTrace();
}
}
我的 JSON 结构是:
{ "items" :
[
{
"code" : "1234",
"type" : "Pagnerine Cialda",
"name" : "Pagnerine Cialda Sottozero/Estate",
"size" : "ct"
},
{
"code" : "1234",
"type" : "Pagnerine Cartone",
"name" : "Pagnerine Cartone 16B",
"size" : "ct"
},
{
"code" : "1234",
"type" : "Zucchero",
"name" : "Saccarosio",
"size" : "kg"
},
]
}