有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中删除嵌套的JSONObject键和值

我需要在java程序中做一些更改,但我在这方面遇到了一些问题。我想从JSONObject中删除“deviceType”:“NORMAL”,但我找不到正确的方法。你能帮我看看这个吗

我已经试过了:

String key = "deviceType";
boolean isFound = jsonObj.containsValue(key);
{
        jsonObj.remove( "deviceType");

}

当我试图运行它时,deviceType没有被删除

这是执行任务的代码块:

public Arglist displayAndUpdateDevice( Arglist argList )
        throws Exception {
        Arglist responseArglist = new Arglist();

        JSONObject parameterJsonValue = new JSONObject();
        parameterJsonValue.put( Constants.ID_STRING, argList.getArg( Constants.DEVICE_ID ) );
        ClientResponse jerseyRestClientResponse = getDataResponse( argList.getArg( Constants.DISPLAY_URI ), parameterJsonValue.toString() );

        JSON obj = Utilities.handleResponse( jerseyRestClientResponse, responseArglist, macro, Constants.HTTP_STATUS_OK, false );
        JSONObject jsonObj = null;
        if ( obj instanceof JSONArray ) {
            jsonObj = ( (JSONArray) obj ).getJSONObject( 0 );
            Utilities.retainAttributes( jsonObj, Constants.UPDATE_DEVICE_PARAMS );
            System.out.println(jsonObj);
        }

        if ( jsonObj != null ) {
            // Handle change IMSI or change MSISDN
            int index = 1;
            JSONArray identities = null;
            while ( argList.containsArg( Constants.DEVICE_IDENTITY_TYPE_PREFIX + index ) ) {
                String arg = argList.getArg( Constants.DEVICE_IDENTITY_TYPE_PREFIX + index );
                if ( identities == null ) {
                    identities = (JSONArray) jsonObj.get( Constants.IDENTITIES_ATTR_STRING );
                }
                // FIXME - Need handling of null for identities
                Iterator<JSONObject> identitiesItr = identities.iterator();
                while ( identitiesItr.hasNext() ) {
                    JSONObject identity = identitiesItr.next();
                    if ( arg.equalsIgnoreCase( (String) identity.get( "identityType" ) ) ) {
                        identity.put( "value", argList.getArg( Constants.DEVICE_IDENTITY_VALUE_PREFIX + index ) );
                        break;
                    }
                }
                index++;
            }

            index = 1;
            JSONArray groups = null;
            JSONObject grp = null;
            while ( argList.containsArg( Constants.GROUP_ID_PREFIX + index ) ) {
                String arg = argList.getArg( Constants.GROUP_ID_PREFIX + index );
                if ( groups == null ) {
                    Object grpObj = jsonObj.get( Constants.GROUPS_STRING );
                    if ( grpObj == null ) {
                        groups = new JSONArray();
                        jsonObj.put( Constants.GROUPS_STRING, groups );
                    } else {
                        groups = (JSONArray) grpObj;
                    }
                }

                grp = new JSONObject();
                grp.put( Constants.ID_STRING, arg );
                grp.put( Constants.NAME_STRING, argList.getArg( Constants.GROUP_NAME_PREFIX + index ) );
                groups.add( grp );
                index++;
            }


            // delete subscriber from group. now only handle 1 group
            if ( argList.containsArg( Constants.UNSUBSCRIBE_FROM_GROUP ) ) {
                String arg = argList.getArg( Constants.UNSUBSCRIBE_FROM_GROUP );
                if ( Boolean.TRUE.toString().equalsIgnoreCase( arg ) ) {
                    jsonObj.remove( Constants.GROUPS_STRING );
                }
            }

            // create rest URL
            String apiUrl = this.address + argList.getArg( Constants.URI );

            // Log In-coming Payload
            Utilities.printRequestInMMLLog( this.macro, jsonObj, apiUrl, null, RequestType.PUT );

            // call REST API
            jerseyRestClientResponse = RestClient.sendRequest( RequestType.PUT, apiUrl, null, jsonObj, userName, password );

            // Log out-going REST Response
            Utilities.printResponseInMMLLog( this.macro, jerseyRestClientResponse );

            Utilities.handleResponse( jerseyRestClientResponse, responseArglist, macro );

        }

        macro.printMml( "Resulting arglist : " + responseArglist );

        return responseArglist;

    }


i expect "deviceType":"NORMAL" is removed from JSONObject


  {
    "modifiedBy": "smadmin",
    "modifiedDate": 1568723161882,
    "lifeCycleNames": {

.
.
.
.
.
.
.


"devices": [
          {
            "modifiedBy": null,
            "modifiedDate": 1568948940927,
            "lifeCycleNames": {
              "entry": []
            },
            "states": {
              "entry": []
            },
            "timeOfDay": null,
            "deviceType": "NORMAL",   <------Need to remove this
            "identities": [],
            "refreshApiStatus": null,
            "bucketInstanceList": []
          }
        ],
.
.
.
.
.
.
    "detailedQuery": false,
    "updateSession": 0,
    "updateSessionResult": 0,
    "counterInstancesList": null,
    "bucketDefinitionMap": null,
    "refreshApiStatus": null,
    "bucketInstanceList": []
  }

共 (1) 个答案

  1. # 1 楼答案

    您没有指向要删除的密钥。它出现在“设备”中 这是一个数组,你试图在外部对象中移除它

      jsonObj.getAsJsonObject("devices[0]").remove("deviceType");