【问题标题】:Can you embed "commands" in JSON? [closed]你能在 JSON 中嵌入“命令”吗? [关闭]
【发布时间】:2021-01-04 19:35:59
【问题描述】:

我有一些 JSON(我们称之为 json1),我想将它与其他一些 JSON (json2) 结合起来。我不只是想合并或附加数据,我还希望能够通过在json2 中嵌入“命令”来删除json1 中的数据。

例子

json1 = { 
  "a": 1,
  "b": "xyz",
  "c": ["foo", "bar"]
}

json2 = {
  "b": "hello world",
  "d": 4,
  "c": ["-foo", "+foobar"],
}

output = combine(json1, json2) 

// Expected result from the fictional "combine()" method
output = {
  "a": 1,
  "b": "hello world",
  "c": ["bar", "foobar"],
  "d": 4
}

当然我可以提出自己的语法和逻辑,但我很好奇是否有任何现有的技术可以使用 JSON 来操作 JSON?

免责声明:我不是在问recommendations for books, tools, software libraries (or more)。我只有与a specific programming problem 相关的a practical, answerable problem that is unique to software development,并且正在询问software tools commonly used by programmers,或者可能是software algorithm。天哪。

【问题讨论】:

标签: php json php-7


【解决方案1】:

使用jsonpatch,您的示例将转换为类似于此的JSON Patch

[
  { "op": "replace", "path": "/b", "value": "hello world" },
  { "op": "add", "path": "/d", "value": 4 },
  { "op": "replace", "path": "/c/0", "value": "foobar"}
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2018-07-02
    • 2013-10-24
    相关资源
    最近更新 更多