【问题标题】:JSON.Stringify | Output the result on the screen with quote tags ''JSON.Stringify |在屏幕上输出带有引号标签''的结果
【发布时间】:2019-09-26 00:49:48
【问题描述】:

我在我的 React 应用程序中使用以下方法将我的 Object 转换为字符串。一切正常,但我想在屏幕上输出结果,而输出周围没有""

  renderConfigInfoCellRow = configInfoKey => {
    const { lconfigInfo } = this.props;
    return (
      <tr key={configInfoKey}>
        <td className="pt-3 text-muted font-weight-bold w-25">{configInfoKey}</td>
        {(typeof lconfigInfo.sql[configInfoKey] === 'string' && (
          <td className="pt-3">{JSON.stringify(lconfigInfo.sql[configInfoKey])}</td>
        )}
      </tr>
    );
  };

我在这里使用它:

<tbody>
  {Object.keys(lconfigInfo.sql).map(configInfoKey =>
    this.renderConfigInfoCellRow(configInfoKey)
  )}
</tbody>

知道如何删除"output" 周围的"" 吗?或者,如果它们是 JSON.Stringify 的替代品。谢谢!!

【问题讨论】:

  • 能否请您提供对象输出示例?而实际结果是什么

标签: javascript json reactjs stringify


【解决方案1】:

使用JSON.stringify()没有意义,如果数据不是对象,尝试改变 这个JSON.stringify(lconfigInfo.sql[configInfoKey])lconfigInfo.sql[configInfoKey]

还有一种情况,您检查lconfigInfo.sql[configInfoKey] 是否为字符串并尝试再次将其转换为字符串。

【讨论】:

    【解决方案2】:

    JSON.stringify 输出有效的 JSON,所以如果你传递一个字符串,它会返回一个用"" 包裹的字符串。

    您正在检查 lconfigInfo.sql[configInfoKey] 的值是否为字符串,因此您可以直接将其打印出来:

    <td className="pt-3">{lconfigInfo.sql[configInfoKey]}</td>
    

    【讨论】:

      【解决方案3】:

      试试.replace(/\"/g, ""):

      {JSON.stringify(lconfigInfo.sql[configInfoKey]).replace(/\"/g, "")}
      

      【讨论】:

        猜你喜欢
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 2023-03-04
        • 2015-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多