gpt4 book ai didi

javascript - 如何在解析 json 时跳过
或任何字符串

转载 作者:行者123 更新时间:2023-11-30 15:55:39 24 4
gpt4 key购买 nike

作为 jquery ajax 调用的结果,我有 json 数组对象。我在我的 python 代码中使用了“< br >”标签来格式化我在 DOM 中的数据。我想跳过这个“
”标签,因为我正在使用 jspdf 和 js 自动表将 ajax 响应 json 数据转换为 pdf 文件。有没有一种简单的方法可以在遍历 json 数组对象时跳过一组字符串(在本例中为“
”)。这是 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"}

enter image description here

最佳答案

我刚刚找到了问题的答案。最简单的方法是使用 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 对象。

关于javascript - 如何在解析 json 时跳过 <br> 或任何字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38597994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com