我使用JQuery来获取Json数据,但它显示的数据有双引号。它有一个功能来删除它?从Json中删除双引号返回数据使用Jquery
$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))
返回:
"House"
如何删除双引号?干杯。
我使用JQuery来获取Json数据,但它显示的数据有双引号。它有一个功能来删除它?从Json中删除双引号返回数据使用Jquery
$('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
的意思是 “全球”(全部替换)。
太棒了!这正是我所寻找的。我替换双引号,并尝试使用一些字符。太好了!谢谢!! – AlvaroAV
正则表达式虽然有效,但会造成大量内存浪费。我认为用户应该不使用JSON.stringify(),或者如果它是只是一个句子的JSON响应,请使用JSON.parse()。当事物规模扩大时,这些正则表达式会加起来。只是说。 –
不修剪开始/结束引号。它剥去所有报价。 –
stringfy
方法不适用于解析JSON,它用于将对象转换为JSON字符串。
JSON在加载时由jQuery解析,您不需要解析数据以使用它。只需使用字符串中的数据:
$('div#ListingData').text(data.data.items[0].links[1].caption);
我不认为有必要更换任何报价,这是一个五脏俱全的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
}
你在做什么是使你的榜样JSON字符串。请不要使用JSON.stringify()
或者如果您有JSON数据回来并且您不需要报价,只需使用JSON.parse()
删除有关JSON响应的引用!不要使用正则表达式,不需要。
利基需要,当你知道喜欢你比如你的数据...这个工程:
JSON.parse(this_is_double_quoted);
JSON.parse("House"); // for example
我也有这个问题,但在我的情况下,我不想使用正则表达式,因为我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
是我的关键字,具有多行值。
这正是我刚才的工作......非常有帮助,谢谢。 :) – dav
javascript替换函数应该可以工作 –