1
[{"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

"period":""

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

3
  • This should probably be tagged with javascript. Commented Jun 23, 2016 at 11:07
  • Is this in Java or JavaScript? Commented Jun 23, 2016 at 11:11
  • If it is for JAVA then pls specify which library you have used to parse. Commented Jun 23, 2016 at 11:22

2 Answers 2

1

See you can do using this way

This is for PHP guy :)

    $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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping:)
0

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

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:

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.

1 Comment

Thanks I will try that out:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.