簡體   English   中英

使用 Jquery 從 Json 返回數據中刪除雙引號

[英]remove double quotes from Json return data using Jquery

我用JQuery得到Json數據,但是它顯示的數據有雙引號。 它有一個 function 刪除它?

$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))

它返回:

"House"

如何刪除雙引號? 干杯。

使用replace

var test = "\"House\"";
console.log(test);
console.log(test.replace(/\"/g, ""));  

// "House"
// House

注意末尾的g表示“全局”(全部替換)。

當您像示例一樣了解數據時,對於利基需求......這有效:

JSON.parse(this_is_double_quoted);

JSON.parse("House");  // for example

stringfy方法不是用於解析 JSON,而是用於將對象轉換為 JSON 字符串。

JSON 在加載時由 jQuery 解析,您無需解析數據即可使用它。 只需使用數據中的字符串:

$('div#ListingData').text(data.data.items[0].links[1].caption);

這里有人建議使用eval()從字符串中刪除引號。 不要那樣做,那只是乞求代碼注入。

我沒有看到這里列出的另一種方法是使用:

let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message))              // Hello World

我也有這個問題,但就我而言,我不想使用正則表達式,因為我的 JSON 值可能包含引號。 希望我的回答能在未來對其他人有所幫助。

我通過使用標准字符串切片刪除第一個和最后一個字符來解決這個問題。 這對我JSON.stringify() ,因為我在生成它的textarea上使用了JSON.stringify() ,因此,我知道我總是會在字符串的每一端都有" s。

在這個通用示例中, response是我的 AJAX 返回的 JSON 對象, key是我的 JSON 密鑰的名稱。

response.key.slice(1, response.key.length-1)

我像這樣使用正則表達式replace來保留換行符並將該鍵的內容寫入我的 HTML 中的段落塊:

$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/\\n/g, '<br/>'));

在這種情況下, $('#description')是我要寫入的段落標記。 studyData是我的 JSON 對象,而description是我的多行值的鍵。

您正在做的是在您的示例中制作一個 JSON 字符串。 要么不要使用JSON.stringify()或者如果您確實有 JSON 數據返回並且您不想要引用,只需使用JSON.parse()刪除圍繞 JSON 響應的引用! 不要使用正則表達式,沒有必要。

你可以簡單的試試 String(); 刪除引號。

請參閱此處的第一個示例: https : //www.w3schools.com/jsref/jsref_string.asp

稍后再謝謝我。

PS:致 MOD:不要誤以為我在挖掘死舊的問題。 我今天遇到了這個問題,我在尋找答案時遇到了這篇文章,我只是發布了答案。

我認為不需要替換任何引號,這是一個完美的 JSON 字符串,您只需要將 JSON 字符串轉換為對象即可。這篇文章完美地說明了情況: 鏈接

例子 :

success: function (data) {

        // assuming that everything is correct and there is no exception being thrown
        // output string {"d":"{"username":"hi","email":"hi@gmail.com","password":"123"}"}
        // now we need to remove the double quotes (as it will create problem and 
        // if double quotes aren't removed then this JSON string is useless)
        // The output string : {"d":"{"username":"hi","email":"hi@gmail.com","password":"123"}"}
        // The required string : {"d":{username:"hi",email:"hi@gmail.com",password:"123"}"}
        // For security reasons the d is added (indicating the return "data")
        // so actually we need to convert data.d into series of objects
        // Inbuilt function "JSON.Parse" will return streams of objects
        // JSON String : "{"username":"hi","email":"hi@gmail.com","password":"123"}"
        console.log(data);                     // output  : Object {d="{"username":"hi","email":"hi@gmail.com","password":"123"}"}
        console.log(data.d);                   // output : {"username":"hi","email":"hi@gmail.com","password":"123"} (accessing what's stored in "d")
        console.log(data.d[0]);                // output : {  (just accessing the first element of array of "strings")
        var content = JSON.parse(data.d);      // output : Object {username:"hi",email:"hi@gmail.com",password:"123"}" (correct)
        console.log(content.username);         // output : hi 
        var _name = content.username;
        alert(_name);                         // hi

}

我在 Promise 中遇到了類似的情況,剛剛解決了將強制轉換為 (String)

export async function getUserIdByRole(userRole: string): Promise<string> {
  const getRole = userData.users.find((element) => element.role === userRole);

  return String (getRole?.id);
}

我使用了這個網站並為 Keys https://csvjson.com/json_beautifier選擇了無引號?

暫無
暫無

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

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