【问题标题】:Remove a String from Json Object从 Json 对象中删除字符串
【发布时间】:2019-04-15 13:57:48
【问题描述】:

我正在尝试将 Avro Kafka 事件转换为 Json,并且需要通过从该事件中删除字符串来操作 Avro 事件。

我正在使用 GSON 库来操作 Json 字符串对象,但不知何故它没有删除预期的字符串。

JsonElement je = new Gson().fromJson(matchRequest, JsonElement.class);
        JsonObject jo = je.getAsJsonObject();
        jo.remove("com.XXX.XXXX");
        jo.remove("com.XXX.XXX");
        jo.remove("com.XXX.XXX");

        System.out.println("#################"+jo);

Json String I am Receiving as matchRequest is
{"interaction_id":"321","customer_id":"32","context_id":"123","work_id":"ewq","work_request_id":"213","task_type":"123","match_type":"wert","resource_list":{"com.xxx.xxxx":{"rank":1,"resource_data":{"com.xxx.xxxx":{"account_id":1,"source_name":"Mankind","channel_id":"voice"}}}},"service_list":{"com.xxx.xxxx":{"rank":5,"priority":1,"resource_count":"gvk","min_proficiency":"10","max_proficiency":"1","attributes":{"com.xxx.xxxx":{"category":"edw","value":"33232"}},"resource_offered":{"com.xxx.xxxx":{"agent_id":"rewq","account_id":"123","source_name":"wqe","resource_address":"ewq","source_address":"rewq","channel_id":"212","channel_provider":"wqe"}}}},"matching_state":"OFFERED"}

JSON 格式

    "interaction_id": "321",
  "customer_id": "32",
  "context_id": "123",
  "work_id": "ewq",
  "work_request_id": "213",
  "task_type": "123",
  "match_type": "wert",
  "resource_list": {
    "com.XXXXXX": {
      "rank": 1,
      "resource_data": {
        "com.XXXX": {
          "account_id": 1,
          "source_name": "Mankind",
          "channel_id": "voice"
        }
      }
    }
  },
  "service_list": {
    "com.XXXX": {
      "rank": 5,
      "priority": 1,
      "resource_count": "gvk",
      "min_proficiency": "10",
      "max_proficiency": "1",
      "attributes": {
        "com.XXXX": {
          "category": "edw",
          "value": "33232"
        }
      },
      "resource_offered": {
        "com.XXXX": {
          "agent_id": "rewq",
          "account_id": "123",
          "source_name": "wqe",
          "resource_address": "ewq",
          "source_address": "rewq",
          "channel_id": "212",
          "channel_provider": "wqe"
        }
      }
    }
  },
  "matching_state": "OFFERED"
}

【问题讨论】:

  • 如果你执行jo.get("com.XXX.XXXX"),你得到预期的内容了吗?
  • 您应该在 resource_list 属性上执行操作,而不是在根目录本身上
  • 您的JSON 有效负载中没有密钥com.XXX.XXXX。你想把它当作JSONPath吗?也许你应该使用JSONPath 库。尝试通过在删除键后提供结果JSON 来阐明您要删除哪些节点。现在还不够清楚。

标签: java json avro


【解决方案1】:

当您查看remove() 方法的文档时,它需要一个来自参数中 JSON 对象的键。

但是,您收到的 JSON 不包含“com.xxx.xxxx”作为键,而是包含一些键,例如“resource_list",链接到另一个包含 "com.xxx.xxxx" 作为键的 JSON 对象。

您可能希望递归查看您收到的 JSON 对象以删除预期的 String

【讨论】:

    【解决方案2】:

    您需要对数组执行操作:

    jo.getAsJsonObject("resource_list").remove("com.XXX.XXXX");
    

    这应该可以解决问题。

    【讨论】:

    • 这会删除整个 JSON "resource_list" 而不仅仅是预期的字符串。
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2021-10-29
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多