简体   繁体   English

如何在java中删除jsonarray中的特定json对象

[英]How to delete specific json object in jsonarray in java

I have json array from server response like this.我有这样的服务器响应的 json 数组。

[
    {
      "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我只想以编程方式知道这个对象在 jsonarray 中的位置

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

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

You forget , in each object in json string after each data like below : 您会忘记,在json字符串中的每个对象中,每个数据都如下所示:

{
          "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 通过在idApp获得位置匹配003的方式,我们可以使用Gson库http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

Add dependency in pom.xml : 在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 : 创建类似于您的json对象的模态类:

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 : 现在将json字符串转换为对象数组并遍历数组并找到匹配对象,如下所示:

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

You can do something like that to remove the specefic jsonObject from jsonArray. 您可以执行类似的操作以从jsonArray中删除特定的jsonObject。

//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)         
        }
     }

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++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM