gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:23:15 25 4
gpt4 key购买 nike

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

我已经尝试过这个:

String key = "deviceType";
boolean isFound = jsonObj.containsValue(key);
{
jsonObj.remove( "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": []
}

最佳答案

您没有指向要删除的键。它存在于“设备”中这是一个数组,您正在尝试将其从外部对象中删除。

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

关于java - 如何在java中删除嵌套的JSONObject键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58319564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com