使用Jquery从Json返回数据中去掉双引号

42

我使用jQuery获取JSON数据,但显示的数据有双引号。是否有函数可以将其去除?

$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))

它返回:

"House"

如何移除双引号?谢谢。


1
JavaScript的替换函数应该可以工作。 - nathan hayfield
9个回答

69

使用replace函数:

var test = "\"House\"";
console.log(test);
console.log(test.replace(/\"/g, ""));  

// "House"
// House

请注意结尾的g表示“全局”(替换所有)。


1
太棒了!这正是我在寻找的。我替换了双引号,并尝试了一些字符。不错!谢谢!! - AlvaroAV
2
正则表达式虽然能够工作,但会占用大量内存。我认为用户要么不使用JSON.stringify(),要么如果JSON响应只是一个句子,就使用JSON.parse()。当事情扩展时,这些正则表达式会增加许多。仅此而已。 - Joshua Michael Calafell
3
不裁剪开头/结尾的引号,而是删除所有引号。 - Yannis

25

对于特定需求,当你像你的例子一样了解自己的数据时,这个方法很有效:

JSON.parse(this_is_double_quoted);

JSON.parse("House");  // for example

8
stringfy 方法不是用于解析 JSON 的,它是将对象转换为 JSON 字符串的方法。
当您加载 JSON 数据时,jQuery 会对其进行解析,因此您不需要对数据进行解析即可使用它。只需在数据中使用字符串即可。
$('div#ListingData').text(data.data.items[0].links[1].caption);

7

有人在这里建议使用eval()从字符串中删除引号。不要这样做,那只会引发代码注入的问题。

另一种没有出现在此处列表中的方法是使用:

let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message))              // Hello World

3

我也有这个问题,但在我的情况下,我不想使用正则表达式,因为我的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

3
你可以尝试使用String();来去除引号。
参考这里的第一个例子: https://www.w3schools.com/jsref/jsref_string.asp 感谢我之后再说。
附言: 给MODs:请不要误解我是在挖掘已经过时的问题。我今天遇到了这个问题,在搜索答案时遇到了这篇文章,我只是贴出了答案。

太好了!使用String()代替JSON.stringify()。 - jcom

1
你所做的是在示例中创建一个 JSON 字符串。要么不使用 JSON.stringify(),要么如果你确实有 JSON 数据返回并且不想要引号,只需使用 JSON.parse() 去除 JSON 响应周围的引号即可!不要使用正则表达式,没有必要。

0

我在一个 Promise 中遇到了类似的情况,通过进行 (String) 强制转换解决了问题。

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

  return String (getRole?.id);
}

0

我认为没有必要替换任何引号,这是一个完美形成的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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接