我有一个JSON数据,该数据一直工作到今天,它包含一些换行符和空格。即使我尝试替换换行符和空格,它也没有将其解析为JSON。有人可以帮我吗?
这是中断的数据,它将其读取为普通字符串。
{"reject_reason":"No","id":"11","address":"xxxx xxx ","telephone":"05555555545","time":"July 24th 2018, 12:16","price":"14.4","type":"Delivery","customer":"1", "name":"xx xxx","postcode":"xxxx","message":"SECOND LEFT ON SALT HILL
CALL WHEN OUTSIDE ","payment":"Cash Payment","customerId":"153"}
第一次编辑:之所以没有将其发布为代码是因为它没有给出换行符。这对移动用户来说更好,但是对不起,您必须像世界上大多数国家一样,是遵守规则而不会质疑的人之一。无论如何
为需要的人而编辑。我首先尝试了JSON.parse和stringify替换,但由于某种原因它不起作用。
解决方案->
try{ myData2 = myData2.replace(/\r?\n|\r/g, " ");
myData2 = JSON.parse(myData2);
}catch(e){}
答案 0 :(得分:-1)
您需要用'\ n'和'\ r'替换新行,并且需要转义这些行才能完全实现json合法(实际上不是真的。。\ r \ n \是json有效,但是需要两次转义才能到达最终目的地,以正确显示新行)。
\\r\\n
示例。
{
"reject_reason": "No",
"id": "11",
"address": "xxxx xxx ",
"telephone": "05555555545",
"time": "July 24th 2018, 12:16",
"price": "14.4",
"type": "Delivery",
"customer": "1",
"name": "xx xxx",
"postcode": "xxxx",
"message": "SECOND LEFT ON SALT HILL\\r\\n\\r\\nCALL WHEN OUTSIDE ",
"payment": "Cash Payment",
"customerId": "153"
}