【问题标题】:How to represent strike-through in JSON [closed]如何在 JSON 中表示删除线 [关闭]
【发布时间】:2016-02-15 09:41:59
【问题描述】:

我正在开发一个用户向服务器发送文本的应用程序。然后服务器更改文本,并以原始文本和修改后的文本进行响应。

如何表示在 JSON 中删除的文本。 (删除线)

比如这样:

我想过这样的事情:

包含所有文本的数组,拆分到包含笔划的位置,以及不包含笔划的位置:

 data={
   textBefore:'this is the first world. this is the second world',
   textAfter:'this is first world. this is second world,
  changes:[
    {'nostroke':'this is th'},
    {'stroe':'efirst world'},
    {'nostroke':'in the third world'}
  ]
}

你有更好的主意吗?

或者像这样:

data={
  textBefore:'this is the first world. this is the second world',
  textAfter:'this is first world. this is second world,
  changes:[
    {from:2,to:22,text:'repace from char 2 to char 22 with this text'},
    {from:2,to:4} // This will only delete 2 chars
    {from:2,to:2,text:'This will only append this text without replae},
  ]

【问题讨论】:

  • JSON 支持字符串、对象、数组、布尔值和其他几种数据类型。它没有内置任何内容来描述这一点,所以答案是“随你喜欢”,这是意见的问题。
  • 我可以问如何在表格中表示某些东西,如何在 UML 图中表示某些东西。所以我在问如何将现实世界的数据转换为 JSON 格式。
  • Then the server <s>change</s> the text 真实世界数据 ?
  • 你的问题可能是个好问题,但你必须发展。添加一些约束和描述。

标签: javascript json strikethrough


【解决方案1】:

我检查了google-diff-match-patch 如何在数组中表示它,这就是结果。 original textoriginal txet 之间的区别:

var x=new diff_match_patch().diff_main('original text','original txet')
JSON.stringify(x)
//Result
"[[0,"original t"],[-1,"e"],[0,"x"],[1,"e"],[0,"t"]]"

解释:

  • 0 两个文本有这个(相同)
  • -1 仅在第一个文本中(已删除)
  • 1 仅在第二个文本中(添加)

源代码:

https://code.google.com/archive/p/google-diff-match-patch/wikis/API.wiki

演示:

https://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

感谢@Andriy 提供链接

【讨论】:

    【解决方案2】:

    您可以在服务器的响应中使用<strike></strike> html 标记(假设您将单词duper 替换为awesome):

    {
        "old":"My super duper text",
        "new":"My super <strike>duper</strike> awesome text"
    }
    

    然后你必须解释它在你的客户端代码中有 html。

    【讨论】:

      【解决方案3】:

      您可以根据需要构建自己的 sheme...

      可能是:

      message: {
          text: 'Then the server change the text',
          strikeAt: 16,
          strikeLen: 6,
          otherFunProperty: 'yNot'
      }
      

      甚至更好:

      message: {
          text: 'Then the server change the text',
          strikes: [ [ 16, 6 ] ],
          bolds: [],
          otherFunProperty: 'yNot'
      }
      

      但这只是一种方式...

      也许不是更适合您的具体情况。

      如果您必须存储某种历史记录,您可以查看diff -u shell 命令(用于patch 命令):

      --- stringBefore    2016-02-15 11:49:04.493941099 +0100
      +++ stringAfter
      @@ -16,0 +17,3 @@
      +<s>
      @@ -22,0 +26,4 @@
      +</s>
      

      【讨论】:

      • 谁对此投了反对票,为什么??
      【解决方案4】:

      您必须在 AJAX 成功处理程序(我假设您发送 XMLHttpRequests)或后端(在形成新旧文本字符串之后,但在创建 JSON 响应之前)通过包装找到已删除的文本将单词放入strike 标签中。

      找出startend 之间的差异可能非常复杂。但是我建议您检查存在的 SO 问题:

      此外,如果您能够了解后端如何处理提交的字符串,那么服务器执行此类逻辑可能会更容易......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-20
        • 2020-03-01
        • 2017-09-03
        • 2021-08-27
        • 2012-12-19
        • 2020-06-24
        • 2020-01-09
        • 1970-01-01
        相关资源
        最近更新 更多