2013-05-16 352 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