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

Javascript 使用Jquery从Json返回数据中删除双引号,javascript,jquery,Javascript,Jquery,我使用JQuery获取Json数据,但它显示的数据有双引号。有没有删除它的功能 $('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption)) 它返回: "House" 如何删除双引号?干杯。使用: 注意末尾的g表示“全局”(全部替换)。stringfy方法不用于解析JSON,而是用于将对象转换为JSON字符串 加载JSON时,jQuery会解析JSON,使用它不需要解析数据。只需使用数据中的字符串

我使用JQuery获取Json数据,但它显示的数据有双引号。有没有删除它的功能

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

"House"
如何删除双引号?干杯。

使用:


注意末尾的
g
表示“全局”(全部替换)。

stringfy方法不用于解析JSON,而是用于将对象转换为JSON字符串

加载JSON时,jQuery会解析JSON,使用它不需要解析数据。只需使用数据中的字符串:

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

我认为没有必要替换任何引号,这是一个格式完美的JSON字符串,您只需将JSON字符串转换为object即可。本文完美地解释了这种情况:

例如:

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

}

您所做的是在示例中生成一个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)
我将其与regex
replace
一起使用,以保留换行符,并将该键的内容写入HTML中的段落块:

$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/\\n/g, '<br/>'));
$(“#description').html(studyData.description.slice(1,studyData.description.length-1)。替换(/\\n/g,
”);
在本例中,
$(“#description”)
是我要写入的段落标记。
studyData
是我的JSON对象,
description
是我的带有多行值的键。

您可以简单地尝试String();删除引号

请参阅此处的第一个示例:

以后谢谢我

PS:TO MODs:不要误认为我在挖掘一个过时的问题。我今天面对这个问题,我在搜索答案时看到了这篇文章,我只是发布了答案。

有人建议使用
eval()
删除字符串中的引号。不要这样做,那只是乞求代码注入

另一种方法是使用:

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

我在《承诺》中也遇到过类似的情况,刚刚解决了演一个演员的问题(字符串)

导出异步函数getUserIdByRole(userRole:string):Promise{ const getRole=userData.users.find((element)=>element.role==userRole); 返回字符串(getRole?.id); }
javascript替换函数应该很好用!这正是我想要的。我替换了双引号,并尝试了一些字符。很好!谢谢!!正则表达式虽然有效,但会造成大量内存浪费。我认为用户要么不应该使用JSON.stringify()或者,如果只是一个句子的JSON响应,请使用JSON.parse()。这些正则表达式会随着事情的发展而增加。只是说。不会修剪开始/结束引号。它会删除所有引号。这正是我刚才所做的…非常有帮助,谢谢。:)Perfet!使用String()而不是JSON.stringify()
let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message))              // Hello World
export async function getUserIdByRole(userRole: string): Promise<string> {
  const getRole = userData.users.find((element) => element.role === userRole);

  return String (getRole?.id);
}