Ye Htet Nyi
Ye Htet Nyi

Reputation: 21

How to delete specific json object in jsonarray in java

I have json array from server response like this.

[
    {
      "idApp" : "001"
      "AppName" : "Test App 1"
    },

    {
      "idApp" : "002"
      "AppName" : "Test App 2"
    },

    {
      "idApp" : "003"
      "AppName" : "Test App 3"
    },

    {
      "idApp" : "004"
      "AppName" : "Test App 4"
    }

]

i just want to know the position of this object in jsonarray programatically

{
          "idApp" : "003"
          "AppName" : "Test App 3"
}

Upvotes: 1

Views: 3301

Answers (4)

Rehan
Rehan

Reputation: 39

May be late, but it may help to someone

    int index = 0;
    while (index <  yourArray.length()) {
        JSONObject object = yourArray.optJSONObject(index);
        if (object != null) {
            if (/*your condition is true*/ true){
                yourArray.remove(index);
                index--;
            }
        }
        index++;
    }

Upvotes: 0

Zia
Zia

Reputation: 1011

You can do something like that to remove the specefic jsonObject from jsonArray.

//find the value in your jsonarray
      for (int i = 0; i < jsonarray.length; ++i)
      {         
      JSONObject rec =jsonarray.getJSONObject(i);
      //check the condition if the given id is associated with the object then remove it
        if(rec.getString("idApp").equals("your_passedId") 
        {
            jsonarray.remove(i)         
        }
     }

Upvotes: 0

Bhuwan Prasad Upadhyay
Bhuwan Prasad Upadhyay

Reputation: 3056

You forget , in each object in json string after each data like below :

{
          "idApp" : "003",
          "AppName" : "Test App 3"
}

By the way to get postion match 003 in idApp we can use Gson library http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

Add dependency in pom.xml :

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>

Create modal class like your json object :

public class Modal {

    private String idApp;
    private String AppName;

    public String getIdApp() {
        return idApp;
    }

    public void setIdApp(String idApp) {
        this.idApp = idApp;
    }

    public String getAppName() {
        return AppName;
    }

    public void setAppName(String AppName) {
        this.AppName = AppName;
    }

}

Now convert json string to array of object and loop over array and find match object like below :

public class JsonUtils {

    public static void main(String[] args) {
        System.out.println("Position for 003 is = " + new JsonUtils().getPositionFromJsonString());
    }

    public int getPositionFromJsonString() {
        Gson gson = new Gson();
        String jsonString = "["
                + "    {"
                + "      \"idApp\" : \"001\","
                + "      \"AppName\" : \"Test App 1\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"002\","
                + "      \"AppName\" : \"Test App 2\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"003\","
                + "      \"AppName\" : \"Test App 3\""
                + "    },"
                + ""
                + "    {"
                + "      \"idApp\" : \"004\","
                + "      \"AppName\" : \"Test App 4\""
                + "    }"
                + ""
                + "]";
        Modal[] modals = gson.fromJson(jsonString, Modal[].class);
        int pos = -1;
        for (Modal m : modals) {
            pos++;
            if ("003".equalsIgnoreCase(m.getIdApp())) {
                return pos;
            }
        }
        return -1;
    }
}

Upvotes: 0

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37594

This should work for you

for(int i = 0 ; i < arguments.length(); i++){
    if(arguments.getObject(i).get("idApp").asString().equals("003"))
    System.out.println("Found it : " + i);
}

Upvotes: 1

Related Questions