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

时间:2015-12-07 10:43:08

标签: java json

我有来自服务器响应的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"
    }

]

我只是想以编程方式知道jsonarray中这个对象的位置

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

3 个答案:

答案 0 :(得分:1)

这应该对你有用

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

答案 1 :(得分:0)

在每个数据之后,你忘记了json字符串中每个对象的,

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

顺便在003中获取位置匹配idApp,我们可以使用Gson库http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

在pom.xml中添加依赖项:

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

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

}

现在将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;
    }
}

答案 2 :(得分:0)

你可以做类似的事情从jsonArray中删除specefic 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)         
        }
     }