2017-11-27 19 views
3
内部ストレージでの私のJSONファイルの

形式が似ている、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私の機能がどうあるべきか、

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(); 
      } 

ある

appointmentIdなどがあり appointmentTitleの値を更新する必要があります私のコードで行う必要がある変更?事前に感謝してください、助けてください。

+0

ちょうどそのよう。オブジェクトのタイトルまたはオブジェクトの値を変更したいですか? – Nero

+0

@Nero値を記載しました – ChyperX

答えて

4

ここにあなたが持っている、あなただけの配列を通過し、あなたのデータを更新する必要があります。私はあなたの質問を理解して

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 
関連する問題