簡體   English   中英

如何在javascript中從我的Json中刪除“?

[英]How to remove " from my Json in javascript?

我正在嘗試將 json 注入到我的backbone.js 應用程序中。 我的 json 有" 對於每一個報價。

有沒有辦法讓我刪除它?
我在下面提供了一個示例:

[{"Id":1,"Name":"Name}]

大概你把它放在一個變量中並且正在使用JSON.parse(data); . 在這種情況下,請使用:

JSON.parse(data.replace(/"/g,'"'));

不過,您可能想要修復您的 JSON 編寫腳本,因為" 在 JSON 對象中無效。

接受的答案是正確的,但是我遇到了麻煩。 當我添加代碼時,檢查調試器,我看到它從

result.replace(/"/g,'"')

result.replace(/"/g,'"')

而不是這個,我用那個:

result.replace(/(&quot\;)/g,"\"")

通過這種表示法,它起作用了。

var data = $('<div>').html('[{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Name}]')[0].textContent;

這應該解析您需要的所有編碼值。

以下對我有用:

function decodeHtml(html) {
    let areaElement = document.createElement("textarea");
    areaElement.innerHTML = html;

    return areaElement.value;
}

我在 Notepad++ 中使用了替換功能並替換了&quot; (不帶引號)帶"並且結果是有效的 json

在我的情況下,“quot”被替換為其他字符,所以我找到了一個愚蠢但有效的解決方法

str.replace(/&qu/g,'').replace(/ot\;/g,'')

這是一種用您需要更改的內容替換 quot 的簡單方法 - 字符、字符串等。

        function solve(input) {
   const replaced = '"' // e.g. replace &quot; by "
   const result = input.replace(/&quot;/g, replaceWith)
   return result;
} 

console.log(solve('{&quot;x&quot;:&quot;1&quot;,&quot;y&quot;:&quot;2&quot;,&quot;z&quot;:&quot;10&quot;}')
   

如果您使用的是 SQL,那么您可以使用:

update tablename set payload = replace(payload, '&', chr(39)) where id = 'unique id';

如果您使用的是 python,那么您可以使用:

import html
l = [{&quot;Id&quot;:1,&quot;Name&quot;:&quot;Name}]
r = html.unescape(l)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM