How to delete JSONArray elements using Java


yaryan997 :

my JsonArray is

[{
"Id": null,
"Name": "One New task",
"StartDate": "2010-02-03T05:30:00",
"EndDate": "2010-02-04T05:30:00",
"Duration": 1,
"DurationUnit": "d",
"PercentDone": 0,
"ManuallyScheduled": false,
"Priority": 1,
"parentId": 8,
"index": 0,
"depth": 3,
"checked": null },{
"Id": null,
"Name": "New task",
"StartDate": "2010-02-04T05:30:00",
"EndDate": "2010-02-04T05:30:00",
"Duration": 0,
"DurationUnit": "d",
"PercentDone": 0,
"ManuallyScheduled": false,
"Priority": 1,
"parentId": 8,
"index": 1,
"depth": 3,
"checked": null }]

Now I want to remove the ID from this JsonArray, ManualScheduled, checked,

I try to use jsonArray.remove(1)and in JAVA jsonArray.discard("Id"). But nothing happened. Am I deleting the array items wrong?

I am using JAVA as technology.

jabclab:

What you have is an array of objects. So you will have to iterate through the array and remove the necessary data from each object , e.g.

for (int i = 0, len = jsonArr.length(); i < len; i++) {
    JSONObject obj = jsonArr.getJSONObject(i);
    // Do your removals
    obj.remove("id");
    // etc.
}

I'm assuming you're using org.json.JSONObjectand org.json.JSONArray, but the body remains the same no matter what JSON processing library you use.

If you want to convert [{"id":215},{"id":216}]to [215,216], you can do this:

JSONArray intArr = new JSONArray();
for (int i = 0, len = objArr.length(); i < len; i++) {
    intArr.put(objArr.getJSONObject(i).getInt("id"));
}

Related


How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in a list using delete order?

Ding medicine I have a list like this lst = [1,3,5]and a main list like thislost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ] I want to remove elements in the second list based on the index in the first list. This means I have to remove 'cat', 'you'

How to remove elements from javax.json.JsonArray in java?

Crutick As per my requirement, I am reading a JsonArray (javax.json.JsonArray) from a local file like this: JsonReader reader = Json.createReader(new FileReader(path)); JsonArray regAttribArr = reader.readObject().getJsonObject("Entries") .getJ

How to access JSONArray elements in JSP

Kyriakos Geroudes my JSONArray is [ {"id":1, "programs_idprograms":3, "description":"course description", "idCourse":"course title" }, {"id":2, "programs_idprograms":3, "description":"course description2", "idCourse":"course ti

How to store unique elements in JSONArray

username I'm trying to store in JSONArray only unique elements fetched from database HashSet<String> hs = new HashSet<String>(); JSONArray mainarray=new JSONArray(); PreparedStatement stmt = connection.prepareStatement("SELECT * from categories"); ResultSet r

How to access JSONArray elements in JSP

Kyriakos Geroudes my JSONArray is [ {"id":1, "programs_idprograms":3, "description":"course description", "idCourse":"course title" }, {"id":2, "programs_idprograms":3, "description":"course description2", "idCourse":"course ti

How to store unique elements in JSONArray

username I'm trying to store in JSONArray only unique elements fetched from database HashSet<String> hs = new HashSet<String>(); JSONArray mainarray=new JSONArray(); PreparedStatement stmt = connection.prepareStatement("SELECT * from categories"); ResultSet r

How to store unique elements in JSONArray

username I'm trying to store in JSONArray only unique elements fetched from database HashSet<String> hs = new HashSet<String>(); JSONArray mainarray=new JSONArray(); PreparedStatement stmt = connection.prepareStatement("SELECT * from categories"); ResultSet r

How to sort JSONArray in JAVA

kumhimanshu449: How to sort a JSONArray of objects by their fields? enter: [ { "ID": "135", "Name": "Fargo Chan" }, { "ID": "432", "Name": "Aaron Luke" }, { "ID": "252", "Name": "Dilip Singh" } ]; Desired output (sorted by "name" field): [ { "

How to sort JSONArray in JAVA

kumhimanshu449: How to sort a JSONArray of objects by their fields? enter: [ { "ID": "135", "Name": "Fargo Chan" }, { "ID": "432", "Name": "Aaron Luke" }, { "ID": "252", "Name": "Dilip Singh" } ]; Desired output (sorted by "name" field): [ { "

How to sort JSONArray in JAVA

kumhimanshu449 How to sort a JSONArray of objects by their fields? enter: [ { "ID": "135", "Name": "Fargo Chan" }, { "ID": "432", "Name": "Aaron Luke" }, { "ID": "252", "Name": "Dilip Singh" } ]; Desired output (sorted by "name" field): [ { "I

How to sort JSONArray in JAVA

kumhimanshu449: How to sort a JSONArray of objects by their fields? enter: [ { "ID": "135", "Name": "Fargo Chan" }, { "ID": "432", "Name": "Aaron Luke" }, { "ID": "252", "Name": "Dilip Singh" } ]; Desired output (sorted by "name" field): [ { "

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in csproj using PowerShell

Zahar I try to remove the element <ItemGroup>from the .csprojfile but my script does nothing. What am I doing wrong? script: [xml]$csproj = Get-Content -Path ".\PackageTest.csproj" $refs = $csproj.Project.ItemGroup | Where-Object {$_.Content.Include -like "Pk

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Dazik I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to delete all elements in String array in Java?

Kartik: I want to delete all elements in Stringan array like: String example[]={"Apple","Orange","Mango","Grape","Cherry"}; Is there any easy way to do it, thanks. Nate W .: If it exampleis not final so simple reassign will work: example = new String[example

How to delete all elements in String array in Java?

Kartik: I want to delete all elements in Stringan array like: String example[]={"Apple","Orange","Mango","Grape","Cherry"}; Is there any easy way to do it, thanks. Nate W .: If it exampleis not final so simple reassign will work: example = new String[example

How to delete all elements in String array in Java?

Kartik: I want to delete all elements in Stringan array like: String example[]={"Apple","Orange","Mango","Grape","Cherry"}; Is there any easy way to do it, thanks. Nate W .: If it exampleis not final so simple reassign will work: example = new String[example