如何根据特定键的值null删除JSON数组中的JSON对象?

时间:2021-05-15 21:06:18
[{"period":"","billing_mode":"checked","price":"1500","bundleid":0,"name":"hello stack","deviceid":"923is3j4","date":"2016-06-23","userid":0,"type":"mobile","strutid":999}]

I have this JSON Array inside which JSON Objects like above were there. I want JSON Objects which has

我有这个JSON数组,里面有JSON对象。我想要JSON对象

"period":""

to get deleted.Is it possible?Please help me thanks. P.S:This is for Java not Javascript.

被删除。有可能吗?请帮助我谢谢。 P.S:这是针对Java而不是Javascript。

2 个解决方案

#1


0  

If you want in java then using java-json.jar you can achieve it as following:

如果你想在java中然后使用java-json.jar你可以实现如下:

public static void main(String[] args) throws JSONException {

        String s="{\"period\":\"\",\"billing_mode\":\"checked\",\"price\":\"1500\",\"packageid\":0,\"name\":\"hello stack\",\"subscriberid\":\"9283is3j4\",\"date\":\"2016-06-23\",\"serviceid\":0,\"type\":\"event\",\"programid\":999}";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray nameArray = jsonObj.names();

        List<String> keyList = new ArrayList<String>();
        for (int i = 0; i < nameArray.length(); i++) {
            keyList.add(nameArray.get(i).toString());
        }

            for (String key : keyList) {
                if (jsonObj.get(key).equals("")) 
                {
                    jsonObj.remove(key);
                }
            }

        System.out.println(jsonObj.toString());
    }

If we remove a JSON Key While Iterating JSONArray then it will give following exception:

如果我们在迭代JSONArray时删除JSON键,那么它将给出以下异常:

Exception in thread "main" java.util.ConcurrentModificationException

So i have achieved by copying the JSON array into a Java collection and used the JSONObject.names() method to obtain a JSONArray of keys.

所以我通过将JSON数组复制到Java集合并使用JSONObject.names()方法获取密钥的JSONArray来实现。

#2


1  

See you can do using this way

看你可以用这种方式做

This is for PHP guy :)

这是为PHP家伙:)

    $json='{"period":"","billing_mode":"checked","price":"1500","packageid":0,"name":"hello stack","subscriberid":"9283is3j4","date":"2016-06-23","serviceid":0,"type":"event","programid":999}';

    $arr = json_decode($json, true);
    unset($arr['period']);
    echo json_encode($arr);

#1


0  

If you want in java then using java-json.jar you can achieve it as following:

如果你想在java中然后使用java-json.jar你可以实现如下:

public static void main(String[] args) throws JSONException {

        String s="{\"period\":\"\",\"billing_mode\":\"checked\",\"price\":\"1500\",\"packageid\":0,\"name\":\"hello stack\",\"subscriberid\":\"9283is3j4\",\"date\":\"2016-06-23\",\"serviceid\":0,\"type\":\"event\",\"programid\":999}";

        JSONObject jsonObj = new JSONObject(s);

        JSONArray nameArray = jsonObj.names();

        List<String> keyList = new ArrayList<String>();
        for (int i = 0; i < nameArray.length(); i++) {
            keyList.add(nameArray.get(i).toString());
        }

            for (String key : keyList) {
                if (jsonObj.get(key).equals("")) 
                {
                    jsonObj.remove(key);
                }
            }

        System.out.println(jsonObj.toString());
    }

If we remove a JSON Key While Iterating JSONArray then it will give following exception:

如果我们在迭代JSONArray时删除JSON键,那么它将给出以下异常:

Exception in thread "main" java.util.ConcurrentModificationException

So i have achieved by copying the JSON array into a Java collection and used the JSONObject.names() method to obtain a JSONArray of keys.

所以我通过将JSON数组复制到Java集合并使用JSONObject.names()方法获取密钥的JSONArray来实现。

#2


1  

See you can do using this way

看你可以用这种方式做

This is for PHP guy :)

这是为PHP家伙:)

    $json='{"period":"","billing_mode":"checked","price":"1500","packageid":0,"name":"hello stack","subscriberid":"9283is3j4","date":"2016-06-23","serviceid":0,"type":"event","programid":999}';

    $arr = json_decode($json, true);
    unset($arr['period']);
    echo json_encode($arr);