android-如何更新JSON文件数组中的特定对象?

android-如何更新JSON文件数组中的特定对象?,第1张

概述内部存储中json文件的格式如下:{"appointments":[{"appointmentId":"app_001","appointmentTitle":"AppointmentTitle1","appointmentDate":"2017-11-25","appointmentTime":&quo

内部存储中Json文件的格式如下:

{"appointments": [{  "appointmentID": "app_001",  "appointmentTitle": "Appointment Title1",  "appointmentDate": "2017-11-25",  "appointmentTime": "10:30",  "appointmentStatus": "active",  "appointmentType": "meeting",  "reminder": {    "type": "notification",    "time": "10:15",    "status": "off"  },  "appointmentDescription": "blablablablabla1"},{  "appointmentID": "app_002",  "appointmentTitle": "AppointmentTitle2",  "appointmentDate": "2017-11-26",  "appointmentTime": "09:00",  "appointmentStatus": "done",  "appointmentType": "exam",  "reminder": {    "type": "alarm",    "time": "08:45",    "status": "on"  },  "appointmentDescription": "blablablablabla2"}]}

我需要更新具有app_001作为约会ID的约会标题的值

我的职责是

String configfileString = "";            file configfile = new file(getApplicationContext().getExternalfilesDir("/appointments"), "appointments.Json");            try {                configfileString = getStringFromfile(configfile.toString());                JsONObject Json = new JsONObject(configfileString);                JsONArray array = Json.getJsONArray("appointments");            } catch (Exception e) {                e.printstacktrace();            }

我需要对代码进行哪些修改?请帮助,在此先感谢.

解决方法:

在这里,您只需遍历数组并更新数据:

JsONArray array = Json.getJsONArray("appointments");for(int i=0;i < array.length(); i++){    JsONObject object = array.getJsONObject(i);    String ID = object.getString("appointmentID");    if (ID.equals("app_001")){       object.put("appointmentTitle","your value updated");    }}String stringJsON = array.toString();//save your data
总结

以上是内存溢出为你收集整理的android-如何更新JSON文件数组中的特定对象?全部内容,希望文章能够帮你解决android-如何更新JSON文件数组中的特定对象?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1091860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存