在JSONObject Android中编辑键和值
嗨,我有一个从网站传递到JSON对象的JSON提要,我想做的是能够将JSONObject写入.JSON文件,再次检索它并编辑其中的值,例如,如果where是一个“color”键:“brown”。如何将颜色更改为红色,然后再次将其保存到JSON文件中 到目前为止,我已经试过了,但我在写文件时遇到了困难 在我的全球行动中在JSONObject Android中编辑键和值,android,json,Android,Json,嗨,我有一个从网站传递到JSON对象的JSON提要,我想做的是能够将JSONObject写入.JSON文件,再次检索它并编辑其中的值,例如,如果where是一个“color”键:“brown”。如何将颜色更改为红色,然后再次将其保存到JSON文件中 到目前为止,我已经试过了,但我在写文件时遇到了困难 在我的全球行动中 public final static void writeDataToFile(Context activityContext, String writableString, S
public final static void writeDataToFile(Context activityContext, String writableString, String fileName){
FileOutputStream fos=null;
try {
fos=activityContext.openFileOutput("/assets/files/"+ fileName, 0);
fos.write(writableString.getBytes());
} catch (FileNotFoundException e) {
Log.e("CreateFile", e.getLocalizedMessage());
}
catch (IOException e) {
Log.e("CreateFile", e.getLocalizedMessage());
}
finally{
if(fos!=null){
try {
// drain the stream
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在我的活动中
public void apiNetworkCallResponse(){
Log.v("loginForm", "11111111111");
Log.v("loginForm", "apiNetworkCallResponse = " + responseJSON);
String writableString = responseJSON.toString();
GlobalActions.writeDataToFile(this, writableString, "mynetwork.json");
}
错误就在这里
02-15 15:43:59.209: E/AndroidRuntime(7525): java.lang.IllegalArgumentException: File /assets/files/mynetwork.json contains a path separator
您的问题是openFileOutput采用文件名,而不能采用完整路径。此外,您根本不能写入资产。当然,为什么不能呢?你试过常见的嫌疑犯吗,比如JSONObject.put?