【问题标题】:The best way to store styling values in JSON在 JSON 中存储样式值的最佳方式
【发布时间】:2021-02-27 01:45:56
【问题描述】:

IMGUR:https://imgur.com/a/uToKdKp

大家好, 假设我们有一个编辑器的屏幕,就像我们在 Stackoverflow 上的那样来起草一个问题。一旦我们点击提交,后端应该调用方法来生成一个 JSON。我的问题是:

  1. 如何存储 JSON 值?如果我有一个帖子并且我想更改文本的colorfont size,例如,这个字符串JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value) 中的单词JSON 而不会影响字符串的其余部分,那么正确的架构会像下面:
{
    "title": title,
    content: [
        {
            "type": "text",
            "value": "<style="color:red">JSON</style> (JavaScript Object Notation, pronounced /ˈdʒeɪsən/; also /ˈdʒeɪˌsɒn/) is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value)."
        },
        {
            "type": "image",
            "value": "imageId"
        }
    ]
}
  1. 如果我采用上述模式,一旦我从客户端取回 JSON,我预见到遍历整个字符串并解析出必要的样式时会出现问题。因此,我认为我的上述架构没有得到优化。处理这种情况的最佳方法是什么?

  2. JSON 生成的流程:通常,当点击“发布”时,这个编辑器是否会向服务器发送一个 POST 请求,然后由服务器生成 JSON?如果是,那么该 POST 请求中包含什么?

【问题讨论】:

    标签: javascript html css json architecture


    【解决方案1】:

    1

    是的,我认为这种方法非常好,原因如下

    • 如果您使用的是contenteditable div,那么您可以直接通过JavaScript 设置innerHTML 而无需任何解析.....或者您可以使用id 来自定义样式。

    2

    这种方法还不错,因为一切都很容易管理。

    3

    我不知道草稿请求,但在提交时知道,或者它必须发送发布请求。

    可以使用fetch() like.... 完成发布请求。

    fetch(url, {
      method: 'POST', // or 'PUT'
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    })
    // if your api sends a confirmation response
    
    //.then(response => response.json())
    //.then(data => {
    //  console.log('Success:', data);
    //})
    .catch((error) => {
      console.error('Error:', error);
    });
    

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 2021-08-05
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多