我需要在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": []
}
答案 0 :(得分:0)
您没有指向要删除的密钥。它存在于“设备”中 这是一个数组,您正尝试在外部对象中将其删除。
jsonObj.getAsJsonObject("devices[0]").remove("deviceType");