org.json.jsonobject:Java中的JSON KEYS嵌套和列出的JSON键

发布于 2025-02-13 08:32:01 字数 1047 浏览 1 评论 0原文

我试图在JSON对象中重命名密钥名称。我可以在顶级重命名,但如果在另一个对象内部存在的密钥或列表对象中是否存在,则无法重命名。

这是我到目前为止尝试的。

         try {
                jsonObject.put(value, jsonObject.remove(key));
                LOG.trace("Renaming Field {} with Field {}", key, value);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

这是一个示例对象。

 {
      "id": 123,
      "contactInformation": {
        "contact": {
          "email": "[email protected]",
        }
      },
      "supportingCustomer": [
        {
          "supportingCustomerName": "JOHN"
        },
        {
          "supportingCustomerName": "JOHN"
        }

      ]

}

在这里,我可以重命名ID,但是很难重命名supportingCustomerName以及contactInformation.contact.email使用org.json.jsonobject?还是还有其他图书馆,我可以用来重命名密钥。任何帮助都会不胜感激?

我的JSON和KEY没有固定。

I am trying to rename key names in a json object.I can rename at the top level, but unable to rename if the key present inside another object or if it present in a List Object.

This is what i have tried so far.

         try {
                jsonObject.put(value, jsonObject.remove(key));
                LOG.trace("Renaming Field {} with Field {}", key, value);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

Here is an Example Object.

 {
      "id": 123,
      "contactInformation": {
        "contact": {
          "email": "[email protected]",
        }
      },
      "supportingCustomer": [
        {
          "supportingCustomerName": "JOHN"
        },
        {
          "supportingCustomerName": "JOHN"
        }

      ]

}

Here I can rename Id, but having difficulty renaming supportingCustomerName and also contactInformation.contact.email using org.json.JSONObject ? Or is there any other library, i can use to rename keys.Any Help would be appreciated?

My JSON And key are dynamic not fixated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-02-20 08:32:02

终于最终使用了

 implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.7.0' 


@Override
public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) {
  List<String> modified =  path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST));
  if(logger.isDebugEnabled()){
    for (String p : modified) {
      logger.debug("Rename path {} new value {}", p, newKeyName);
    }
  }
  return this;
}

Finally Ended Up using

 implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.7.0' 


@Override
public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) {
  List<String> modified =  path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST));
  if(logger.isDebugEnabled()){
    for (String p : modified) {
      logger.debug("Rename path {} new value {}", p, newKeyName);
    }
  }
  return this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文