6

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 from this JsonArray I want to remove Id, ManuallyScheduled, checked,

I tried using jsonArray.remove(1) and also jsonArray.discard("Id") in JAVA. but nothing happens. what am I doing wrong to remove array items?

I am using JAVA as my technology.

javanna
  • 59,145
  • 14
  • 144
  • 125
yaryan997
  • 483
  • 3
  • 10
  • 18

3 Answers3

9

What you have there is an array of objects. Therefore you'll have to loop 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've assumed you're using org.json.JSONObject and org.json.JSONArray here, but the principal remains the same whatever JSON processing library you're using.

If you wanted to convert something like [{"id":215},{"id":216}] to [215,216] you could so something like:

JSONArray intArr = new JSONArray();
for (int i = 0, len = objArr.length(); i < len; i++) {
    intArr.put(objArr.getJSONObject(i).getInt("id"));
}
jabclab
  • 14,786
  • 5
  • 54
  • 51
  • Glad to hear it :-) You could always extend `JSONArray` and add a `removeData(String... keys)` method or something similar if this was something you wanted to do regularly. – jabclab Jan 05 '12 at 10:15
  • one more question to you.. How can i just pass the value to JSONArray in java. like I am having jsonArray of form **[{"id":215},{"id":216}]** but i want to convert my jsonArray to **[215,216]** is there any way to do so ?? how can i achieve this. – yaryan997 Jan 05 '12 at 12:12
  • You could do this manually, I'll update my answer to show how to do this. – jabclab Jan 05 '12 at 12:17
  • Hi I am having the same problem I am receiving the date in format of **2012-01-30T00:00:00** so how can I assign it to my Date variable startDate in JAVA pojo class. – yaryan997 Jan 12 '12 at 09:12
  • I should think you'll have to use `.getString("date")` and then manually parse the `String` into a `Date` using something like `SimpleDateFormat`. – jabclab Jan 12 '12 at 09:35
  • how to parse the date String i received from String to Date using SimpleDateFormate ?? Do you have any idea.. My String Date is **2012-01-30T00:00:00** – yaryan997 Jan 12 '12 at 09:45
  • If you don't mind about the time (i.e. it's always `00:00:00`) then you could just remove that bit and then parse. e.g. `SimpleDateFormat sdf = new SimpleDateFormat("YY-MM-DD"); Date d = sdf.parse(str.split("T")[0]);` Here's the [SimpleDateFormat](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html) javadoc. – jabclab Jan 12 '12 at 09:49
  • I tried to use your solution but don't know my sysout is not working I am not able to parse using **SimpleDateFormat sdf = new SimpleDateFormat("YY-MM-DD"); Date d = sdf.parse(jsonObject.getString("StartDate").split("T")[0]);** my java console is not giving any error and the line not executing – yaryan997 Jan 12 '12 at 10:16
  • Try `yyyy-MM-dd` for the `SimpleDateFormat` pattern. – jabclab Jan 12 '12 at 10:31
2

This is useful sometimes in android when you want to use the json structure directly.

Notice that I only use this when I'm handling JSONObject inside the array.

public static JSONArray remove(final int idx, final JSONArray from) {
    final List<JSONObject> objs = asList(from);
    objs.remove(idx);

    final JSONArray ja = new JSONArray();
    for (final JSONObject obj : objs) {
        ja.put(obj);
    }

    return ja;
}

public static List<JSONObject> asList(final JSONArray ja) {
    final int len = ja.length();
    final ArrayList<JSONObject> result = new ArrayList<JSONObject>(len);
    for (int i = 0; i < len; i++) {
        final JSONObject obj = ja.optJSONObject(i);
        if (obj != null) {
            result.add(obj);
        }
    }
    return result;
}
Rafael Sanches
  • 1,823
  • 21
  • 28
2

The following method will find the object in an array with the matching id, then return the filtered array.

public JSONArray removeObject(JSONArray array, String id) throws JSONException {
    for (int i = 0; i < array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);
        if (obj.getString("ID").equals(id)) {
            array.remove(i);
            //  Toast.makeText(this, "ENCONTRADO", Toast.LENGTH_SHORT).show();
        }
    }
    return array;
}
Trent
  • 4,208
  • 5
  • 24
  • 46