【问题标题】:How is this json invalid?这个json怎么无效?
【发布时间】:2015-09-30 20:02:54
【问题描述】:

我实际上对这个 json 是如何无效的感到困惑。我有一个来自更大 json 集的 json:

               ...
               "open_until":{  
                  "date":"2015-10-16 00:00:00",
                  "timezone_type":3,
                  "timezone":"America\/Edmonton"
               },
               "ffba_access_code":"1234",
               "overview_content":" 
Elevator Pitch<\/h4>\r\n 
Fedilus Funds gives investors the ability to participate in the growth of the United States un-banked sector by deploying 'automated commerce machines' in high traffic areas. This is a blended investment that distributes monthly principal and interest back to the investor and once the term has ended, a percentage of revenue is distributed continuously. You are purchasing a portfolio of advanced ATM's that offer a wide range of financial services coupled with a strong management team.<\/p>\r\n\r\n 

This is a USD investment with monthly distributions remitted in USD.<\/p>",
               .....

当我把它通过 json 格式化程序时,我被告知:

" 
    Elevator Pitch<\/h4>\r\n 
    Fedilus Funds gives investors the ability to participate in the growth of the United States un-banked sector by deploying 'automated commerce machines' in high traffic areas. This is a blended investment that distributes monthly principal and interest back to the investor and once the term has ended, a percentage of revenue is distributed continuously. You are purchasing a portfolio of advanced ATM's that offer a wide range of financial services coupled with a strong management team.<\/p>\r\n\r\n 

    This is a USD investment with monthly distributions remitted in USD.<\/p>"

是“无效”的“违规”部分

我从 php.ini 中的数据库中获取此权限。当我将其拉出以使其符合要求时,我应该对这个特定的数据库字段做些什么吗?

【问题讨论】:

  • 您是否尝试将htmlspecialchars 用于您的overview_content 文本?
  • 如果我没记错的话,换行符是无效的……
  • @Andre 所以我从数据库中提取了数据,如何使其有效?
  • 通过json_encode。让我反过来问这个问题:为什么要“手动”编码?
  • @Andre:你没记错:json.org/string.gif 换行符是一个控制字符。

标签: php mysql json


【解决方案1】:

您不能在属性值(或为此的属性)中使用换行符。

这个例子 lints 很好:

...
"open_until": {
  "date": "2015-10-16 00:00:00",
  "timezone_type": 3,
  "timezone": "America/Edmonton"
},
"ffba_access_code": "1234",
"overview_content": "Elevator Pitch</h4>\r\n Fedilus Funds gives investors the ability to participate in the growth of the United States un-banked sector by deploying 'automated commerce machines' in high traffic areas. This is a blended investment that distributes monthly principal and interest back to the investor and once the term has ended, a percentage of revenue is distributed continuously. You are purchasing a portfolio of advanced ATM's that offer a wide range of financial services coupled with a strong management team.</p>\r\n\r\n This is a USD investment with monthly distributions remitted in USD.</p>"
...

【讨论】:

    【解决方案2】:

    JSON 不允许真正的换行符。您需要将所有换行符替换为\n。

    【讨论】:

      【解决方案3】:

      关于需要转义的换行符,上面的答案是正确的。查看 php 的内置 json_encode() 函数。

      另外,这个答案有一些关于换行和转义反斜杠的有用信息:Multiline strings in JSON

      【讨论】:

        【解决方案4】:

        正如一些人指出的那样,将来自数据库的内容包装在 htmlspecialchars 中可以解决问题。

        【讨论】:

        • @VolkerK 嗯,我是根据来自数据库的内容完成的
        • 您的意思是问题中显示的错误json已经在数据库中;你从 text/varchar 字段中提取它?在那种情况下:是什么将无效编码(非)json放入数据库中?无论如何,htmlspecialchars() 对换行符/提要字符没有任何作用。此函数“处理”的字符为&amp;"、“", >
        猜你喜欢
        • 2012-02-06
        • 1970-01-01
        • 2012-07-09
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 2020-02-23
        • 1970-01-01
        • 2015-10-10
        相关资源
        最近更新 更多