Android 在JsonObject中打开并编辑值
我在资产文件夹中有本地Json文件 我使用此代码打开文件Android 在JsonObject中打开并编辑值,android,android-file,android-json,Android,Android File,Android Json,我在资产文件夹中有本地Json文件 我使用此代码打开文件 try { is = getAssets().open("data.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); jsonString = is.toString(); jsonSt
try {
is = getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonString = is.toString();
jsonString = new String(buffer,"UTF-8");
myJson = new JSONObject(jsonString);
jsonArrayData = myJson.getJSONArray("diTich");
int leng = jsonArrayData.length();
for(int i = 0 ; i < leng ; i++) {
mTitle = jsonArrayData.getJSONObject(i).getString("title");
mDescription = jsonArrayData.getJSONObject(i).getString("description");
mAddress = jsonArrayData.getJSONObject(i).getString("address");
mStatus = jsonArrayData.getJSONObject(i).getString("status");
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
is=getAssets().open(“data.json”);
int size=is.available();
字节[]缓冲区=新字节[大小];
is.read(缓冲区);
is.close();
jsonString=is.toString();
jsonString=新字符串(缓冲区,“UTF-8”);
myJson=新的JSONObject(jsonString);
jsonArrayData=myJson.getJSONArray(“diTich”);
int leng=jsonArrayData.length();
对于(int i=0;i
我的Json文件
{
“ABCD”:[
{
“标题”:“abcd”,
“说明”:“abc”,
“地址”:“bnc”,
“图像”:“abcg”,
“状态”:错误
}
]
}
我在JsonObject中检索到了值。现在我想编辑这个值
例如,将键“status”的值从false更改为true。
我该怎么做?我不知道写什么来替换它
谢谢你们 使用JSONObject类的
JSONObject.put()
方法。因此,在您的示例中,您可以这样做:
jsonArrayData.getJSONObject(i).put("status", true);
这将破坏当前存在的值。嗨,DavidAdams!我尝试了你的方式,但价值不更新!您知道另一种方法吗?可能是因为JSONObject位于JSONArray中。试试这个:JSONObject newObject jsonArrayData.getJSONObject(i);newObject.put(“状态”,true);如果需要更改JSONArray,那么使用JSONArray.put()方法将新的JSONObject放入索引(i)处的JSONArray中。