使用Jquery从Json返回数据中删除双引号

remove double quotes from Json return data using Jquery

本文关键字:删除 数据 Jquery Json 返回 使用      更新时间:2023-09-26

我使用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表示"global"(替换所有)。

当你像你的例子一样了解你的数据时,对于利基需求…

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值可能包含引号。希望我的回答能在将来对别人有所帮助。

我通过使用标准字符串切片来删除第一个和最后一个字符来解决这个问题。这对我来说是有效的,因为我在产生它的textarea上使用了JSON.stringify(),因此,我知道我总是会在字符串的每个末端使用" 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:给mod:不要误会我在挖掘死去的老问题。我今天遇到了这个问题,我在寻找答案的时候看到了这篇文章,我只是把答案贴出来。

您正在做的是在您的示例中创建一个JSON字符串。要么不要使用JSON.stringify(),或者如果你有JSON数据回来,你不想要引号,只需使用JSON.parse()来删除JSON响应周围的引号!不要使用正则表达式,没有必要。

我不认为有必要替换任何引号,这是一个完美的JSON字符串,你只需要将JSON字符串转换为对象。这篇文章很好地解释了这种情况:Link

的例子:

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
}

我有类似的情况在一个承诺刚刚解决做cast作为(String)

export async function getUserIdByRole(userRole: string): Promise<string> {
  const getRole = userData.users.find((element) => element.role === userRole);
  return String (getRole?.id);
}