我有一个类compositionJSON。该类有一个方法调用makeJSONObject,它创建一个JSON-Object并将东西放入其中。这是该类的代码。
public class CompositionJso extends JSONObject {
public JSONObject makeJSONObject (String title, String desc, ArrayList<String> imgPath, ArrayList<Resources> imgView) {
JSONObject obj = new JSONObject() ;
try {
obj.put("title", title);
obj.put("desc", desc);
obj.put("imgPath", imgPath);
obj.put("imgViewPath", imgView);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
现在我创建了这个类的一个实例,并在另一个类中调用该方法。之后我想将JSONObject写入文件并将其保存在设备上的SD卡上。这是代码:
saveCompo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setName();
createJSONFolder();
CompositionJso obj = new CompositionJso();
obj.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
MyCompositionsListActivity.buildList();
try {
Writer output = null;
File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
output = new BufferedWriter(new FileWriter(file));
output.write(obj.toString());
output.close();
Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
finish();
}
});
文件保存成功,但如果我打开它,里面什么都没有。代码有什么问题?
答案 0 :(得分:7)
makeJSONObject
正在返回JSONObject
您的代码应为
saveCompo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setName();
createJSONFolder();
CompositionJso obj = new CompositionJso();
JSONObject jsonObject = obj.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
MyCompositionsListActivity.buildList();
try {
Writer output = null;
File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
output = new BufferedWriter(new FileWriter(file));
output.write(jsonObject.toString());
output.close();
Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
finish();
}
});
答案 1 :(得分:1)
谢谢@Chris Handy!我创建了一个JSONObjectVariable并将其分配给makeJSONObject。这是我的最终代码:
saveCompo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setName();
createJSONFolder();
CompositionJso compositionJso = new CompositionJso();
JSONObject obj;
obj = compositionJso.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
MyCompositionsListActivity.buildList();
try {
Writer output;
File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
output = new BufferedWriter(new FileWriter(file));
output.write(obj.toString());
output.close();
Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
finish();
}
});
答案 2 :(得分:1)
尝试使用静态方法编写一个简单的class
来保存和检索文件中的json object
:
代码:
public class RetriveandSaveJSONdatafromfile {
public static String objectToFile(Object object) throws IOException {
String path = Environment.getExternalStorageDirectory() + File.separator + "/AppName/App_cache" + File.separator;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
path += "data";
File data = new File(path);
if (!data.createNewFile()) {
data.delete();
data.createNewFile();
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(data));
objectOutputStream.writeObject(object);
objectOutputStream.close();
return path;
}
public static Object objectFromFile(String path) throws IOException, ClassNotFoundException {
Object object = null;
File data = new File(path);
if(data.exists()) {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(data));
object = objectInputStream.readObject();
objectInputStream.close();
}
return object;
}
}
要将json保存在文件中,请使用RetriveandSaveJSONdatafromfile.objectToFile(jsonObj)
并从文件使用中获取数据
path = Environment.getExternalStorageDirectory() + File.separator +
"/AppName/App_cache/data" + File.separator;
RetriveandSaveJSONdatafromfile.objectFromFile(path);
答案 3 :(得分:0)
在方法makeJSONObject中,您实例化一个新的JSONObject。
此代码应该有效。
public void makeJSONObject (String title, String desc, ArrayList<String> imgPath, ArrayList<Resources> imgView) {
try {
this.put("title", title);
this.put("desc", desc);
this.put("imgPath", imgPath);
this.put("imgViewPath", imgView);
} catch (JSONException e) {
e.printStackTrace();
}
}