如何处理JSON中的特殊字符

时间:2019-10-16 17:32:55

标签: json

我有一个网格的列定义,我试图将其放入JSON n负载中。我收到的JSON错误无效的特殊字符和功能很少

[ 
   { 
      "headerName":"",
      "field":"",
      "headerCheckboxSelection":true,
      "checkboxSelection":true,
      "width":45
   },
   { 
      "headerName":"A-1 PORT",
      "width":110,
      "cellRendererFramework":"params => {
          return this.renderCells(params)
        }",
      "field":"a1Port",
      "tooltipField":"A-1 PORT",
      "icons":"{
          sortAscending: `<img src=${ascArrow} style=\"margin-left:65px;color:black; position:absolute\" />`,
          sortDescending: `<img src=${dscArrow} style=\"margin-left:65px;color:black; position:absolute\" />`}"
   }
]

在这种情况下如何转义功能和特殊字符

enter image description here

1 个答案:

答案 0 :(得分:4)

这里的问题是您的cellRendererFrameworkicons属性中的换行符,因为JSON不支持文字换行符。如果您打算将这些属性保留为字符串(而不是将icons属性变成另一个对象),则可以尝试以下操作:

[ 
   { 
      "headerName":"",
      "field":"",
      "headerCheckboxSelection":true,
      "checkboxSelection":true,
      "width":45
   },
   { 
      "headerName":"A-1 PORT",
      "width":110,
      "cellRendererFramework":"params => { return this.renderCells(params) }",
      "field":"a1Port",
      "tooltipField":"A-1 PORT",
      "icons":"{ sortAscending: `<img src=${ascArrow} style=\"margin-left:65px;color:black; position:absolute\" />`, sortDescending: `<img src=${dscArrow} style=\"margin-left:65px;color:black; position:absolute\" />`}"
   }
]

如果要在字符串中保留换行符,则可以在要换行的任何位置插入\n