我使用 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 字符串。
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
产生它的 s ,因此,我知道我总是会"
在字符串的每一端都有 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
是我的具有多行值的键。
您可以简单地尝试 String(); 删除引号。
请参阅此处的第一个示例:https ://www.w3schools.com/jsref/jsref_string.asp
晚点再谢我。
PS:TO MODs:不要误认为我在挖掘死旧的问题。我今天遇到了这个问题,在寻找答案时遇到了这篇文章,我只是发布了答案。
您正在做的是在您的示例中创建一个 JSON 字符串。要么不要使用,JSON.stringify()
要么如果您确实有 JSON 数据返回并且您不想要引号,只需使用JSON.parse()
删除 JSON 响应周围的引号!不要使用正则表达式,没有必要。
我认为不需要替换任何引号,这是一个完美格式的 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);
}