Are multi-line strings allowed in JSON?

前端 未结 9 1312
醉酒成梦
醉酒成梦 2020-11-22 04:21

Is it possible to have multi-line strings in JSON?

It\'s mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I\'m just kinda curious

9条回答
  •  孤街浪徒
    2020-11-22 04:49

    I have had to do this for a small Node.js project and found this work-around:

    {
     "modify_head": [
    
      ""
    
     ],
    
     "modify_body": [
    
      "",
      ""
    
     ],
    }
    

    This looks quite neat to me, appart from that I have to use double quotes everywhere. Though otherwise, I could, perhaps, use YAML, but that has other pitfalls and is not supported natively. Once parsed, I just use myData.modify_head.join('\n') or myData.modify_head.join(), depending upon whether I want a line break after each string or not.

提交回复
热议问题