如何在解析 json 时跳过 <br> 或任何字符串

How to skip a <br> or any string while parsing json

由于 jquery ajax 调用,我有 json 数组对象。 我在 python 代码中使用了“< br >”标记来格式化 DOM 中的数据。我想跳过这个“< br >”标签,因为我正在使用 jspdf 和 js auto table 将 ajax 响应 json 数据转换为 pdf 文件。 有没有一种简单的方法可以在遍历 json 数组对象时跳过一组字符串(在本例中为“< br >”)。 这是 json 数组

中的对象之一
{"content":{"rows":[["25.07. ","ma","","","","10:44 - 10:51 <br>","10:57","0:00"],["26.07. ","ti","","","","","","0:00"],["27.07. ","ke","","","","","","0:00"],["28.07. ","to","","","","","","0:00"],["29.07. ","pe","","","","","","0:00"],["30.07. ","la","","","","","","0:00"],["31.07. ","su","","","","","","0:00"],["01.08. ","ma","","","","","","0:00"],["02.08. ","ti","","","","","","0:00"],["03.08. ","ke","","","","","","0:00"],["04.08. ","to","","","","","","0:00"],["05.08. ","pe","","","","","","0:00"],["06.08. ","la","","","","","","0:00"],["07.08. ","su","","","","","","0:00"]],"data":[["25.07. ","ma","","","","10:44 - 10:51 <br>","10:57","0:00"],["26.07. ","ti","","","","","","0:00"],["27.07. ","ke","","","","","","0:00"],["28.07. ","to","","","","","","0:00"],["29.07. ","pe","","","","","","0:00"],["30.07. ","la","","","","","","0:00"],["31.07. ","su","","","","","","0:00"],["01.08. ","ma","","","","","","0:00"],["02.08. ","ti","","","","","","0:00"],["03.08. ","ke","","","","","","0:00"],["04.08. ","to","","","","","","0:00"],["05.08. ","pe","","","","","","0:00"],["06.08. ","la","","","","","","0:00"],["07.08. ","su","","","","","","0:00"]],"columns":["Pvm","Päivä","Sisään","Ulos työ","Ulos oma asia","Ulos muu","Ulos","Työaika"]},"title":"peter pan 25.07. - 07.08.2016 0:00"}

我刚刚找到了问题的答案。最简单的方法是使用 javascript stringify 方法将 jSON 对象转换为字符串,然后使用 javascript replace 方法替换任何字符串。我将说明我的答案如下。

var jsonObject = someJsonArrayObject; // your json object
var jsonObjectStringify = JSON.stringify(jsonObject).replace(/<br>/g, ""); //convert to JSON string to remove the br tag
var newJsonObject = JSON.parse(jsonObjectStringify); //convert back to array

只需将要替换的字符串与 g 标志一起放在两个正斜杠之间,然后将其替换为您想要的任何字符串。在我的例子中,我不得不删除 br 标签,所以我用空字符串替换了它。 之后,您必须在最后一步将字符串转换回 json 对象。