我使用jQuery获取JSON数据,但显示的数据有双引号。是否有函数可以将其去除?
$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))
它返回:
"House"
如何移除双引号?谢谢。
我使用jQuery获取JSON数据,但显示的数据有双引号。是否有函数可以将其去除?
$('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 字符串的方法。$('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值可能包含引号。希望我的回答能帮助未来的其他人。
我通过使用标准字符串切片来解决此问题,从而删除了第一个和最后一个字符。这对我有用,因为我对产生它的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
是我的键,其具有多行值。JSON.stringify()
,要么如果你确实有 JSON 数据返回并且不想要引号,只需使用 JSON.parse()
去除 JSON 响应周围的引号即可!不要使用正则表达式,没有必要。我在一个 Promise 中遇到了类似的情况,通过进行 (String) 强制转换解决了问题。
export async function getUserIdByRole(userRole: string): Promise<string> {
const getRole = userData.users.find((element) => element.role === userRole);
return String (getRole?.id);
}
我认为没有必要替换任何引号,这是一个完美形成的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
}