首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中删除嵌套的JSONObject键和值

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

Stack Overflow用户
提问于 2019-10-10 17:22:05
回答 1查看 483关注 0票数 0

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

我已经试过了:

代码语言:javascript
复制
String key = "deviceType";
boolean isFound = jsonObj.containsValue(key);
{
        jsonObj.remove( "deviceType");

}

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

下面是包含以下任务的代码块:

代码语言:javascript
复制
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": []
  }
EN

回答 1

Stack Overflow用户

发布于 2019-10-10 17:43:58

您没有指向要删除的键。它出现在“device”中,这是一个数组,您正试图在外部对象中删除它。

代码语言:javascript
复制
  jsonObj.getAsJsonObject("devices[0]").remove("deviceType");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58319564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档