【问题标题】:What is the data type for JSON?JSON 的数据类型是什么?
【发布时间】:2021-01-30 03:43:47
【问题描述】:

我正在根据phpDocumentor格式编写一个php方法。该方法只返回json,我在moemnt 写了String。我想知道这是正确的吗?

/**
 * This is a method returns json
 * @return String
 */
public function _getJsonData() {
  .....
}

【问题讨论】:

    标签: php json document dataformat


    【解决方案1】:

    是的……但不是。

    你说得对,就 PHP 的类型系统而言,返回 JSON 的函数正在返回一个字符串,而这正是 phpDocumentor 所关心的。

    但是,按照惯例,内置类型始终是小写字母,前导大写字母表示类名,因此您应该使用 @return string 而不是 @return String

    另请注意,除非您需要在真正旧版本的 PHP 上运行代码,否则您可以在函数签名本身中指定返回类型:

    public function _getJsonData(): string {
      .....
    }
    

    此时,仅指定@return string 是毫无意义的,但添加字符串的描述 可能更有用:

    /**
     * @return string A string formatted in JSON ready for use with the frobulator
     */
    public function _getJsonData(): string {
      .....
    }
    

    【讨论】:

      【解决方案2】:

      是的,JSON 响应是一个字符串。

      当然,这并没有说明这个字符串值实际传达的“含义”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 2012-05-16
        • 2018-11-04
        • 1970-01-01
        相关资源
        最近更新 更多