I need to populate a json object this way, let it be called detailJSON:
{"amount": "5.00", "ac_no": "123456" }
I do like this:
detailJSON.put("amount","5.00"); detailJSON.put("ac_no","123456");
After that, the detail is entered into some general settings, and now I want to clear this JSONObject and use the same detailJSON object to store another json (with different keys), as follows:
{"amount":"6.00", "loan_no":"123456"}
I know that there is a method, remove (), which removes a specific key and corresponding value.
This works :
detailJSON.remove("amount"); detailJSON.remove("ac_no");
and then use it -
detailJSON.put("amount","6.0"); detailJSON.put("loan_no","123456");
Now this is a very simple example. In the code I'm working on, I have many keys, so using remove actually increases the LOC. In addition, every time before deleting, I need to check if JSONObject has specific key or not.
Is there any other way I can implement JSONObject cleanup ??
I tried
detailJSON=null ; detailJSON=new JSONObject();
But that will not work.
I'm basically looking for something like the clear () method, if one exists.