2013-05-16 256 views
33

我使用JQuery來獲取Json數據,但它顯示的數據有雙引號。它有一個功能來刪除它?從Json中刪除雙引號返回數據使用Jquery

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

返回:

"House" 

如何刪除雙引號?乾杯。

+1

javascript替換函數應該可以工作 –

回答

54

使用replace

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

// "House" 
// House 

註上月底g的意思是 「全球」(全部替換)。

+1

太棒了!這正是我所尋找的。我替換雙引號,並嘗試使用一些字符。太好了!謝謝!! – AlvaroAV

+1

正則表達式雖然有效,但會造成大量內存浪費。我認爲用戶應該不使用JSON.stringify(),或者如果它是隻是一個句子的JSON響應,請使用JSON.parse()。當事物規模擴大時,這些正則表達式會加起來。只是說。 –

+0

不修剪開始/結束引號。它剝去所有報價。 –

5

stringfy方法不適用於解析JSON,它用於將對象轉換爲JSON字符串。

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

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

我不認爲有必要更換任何報價,這是一個五臟俱全的JSON字符串,你只需要JSON字符串轉換成object.This文章完美的破解情況:Link

例子:

success: function (data) { 

     // assuming that everything is correct and there is no exception being thrown 
     // output string {"d":"{"username":"hi","email":"[email protected]","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":"[email protected]","password":"123"}"} 
     // The required string : {"d":{username:"hi",email:"[email protected]",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":"[email protected]","password":"123"}" 
     console.log(data);      // output : Object {d="{"username":"hi","email":"[email protected]","password":"123"}"} 
     console.log(data.d);     // output : {"username":"hi","email":"[email protected]","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:"[email protected]",password:"123"}" (correct) 
     console.log(content.username);   // output : hi 
     var _name = content.username; 
     alert(_name);       // hi 

} 
1

你在做什麼是使你的榜樣JSON字符串。請不要使用JSON.stringify()或者如果您有JSON數據回來並且您不需要報價,只需使用JSON.parse()刪除有關JSON響應的引用!不要使用正則表達式,不需要。

13

利基需要,當你知道喜歡你比如你的數據...這個工程:

JSON.parse(this_is_double_quoted); 

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

我也有這個問題,但在我的情況下,我不想使用正則表達式,因爲我JSON值可能包含引號。希望我的回答將在未來幫助其他人。

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

在這個廣​​義的例子中,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是我的關鍵字,具有多行值。

+0

這正是我剛纔的工作......非常有幫助,謝謝。 :) – dav