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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16598477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:26:22  来源:igfitidea点击:

remove double quotes from Json return data using Jquery

javascriptjquery

提问by Alex

I use JQuery to get Json data, but the data it display has double quotes. It there a function to remove it?

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

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

it returns:

它返回:

"House"

How can I remove the double quote? Cheers.

如何删除双引号?干杯。

回答by McGarnagle

Use replace:

使用replace

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

// "House"
// House

Note the gon the end means "global" (replace all).

注意g最后的意思是“全局”(替换全部)。

回答by Scott Stensland

For niche needs when you know your data like your example ... this works :

当您像示例一样了解数据时,对于利基需求......这有效:

JSON.parse(this_is_double_quoted);

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

回答by Guffa

The stringfymethod is not for parsing JSON, it's for turning an object into a JSON string.

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

The JSON is parsed by jQuery when you load it, you don't need to parse the data to use it. Just use the string in the data:

JSON 在加载时由 jQuery 解析,您无需解析数据即可使用它。只需使用数据中的字符串:

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

回答by Bmd

I also had this question, but in my case I didn't want to use a regex, because my JSON value may contain quotation marks. Hopefully my answer will help others in the future.

我也有这个问题,但就我而言,我不想使用正则表达式,因为我的 JSON 值可能包含引号。希望我的回答能在未来对其他人有所帮助。

I solved this issue by using a standard string slice to remove the first and last characters. This works for me, because I used JSON.stringify()on the textareathat produced it and as a result, I know that I'm always going to have the "s at each end of the string.

我通过使用标准字符串切片删除第一个和最后一个字符来解决这个问题。这对我有用,因为我用JSON.stringify()textarea产生它的那个上,因此,我知道我总是会"在字符串的每一端都有s。

In this generalized example, responseis the JSON object my AJAX returns, and keyis the name of my JSON key.

在这个通用示例中,response是我的 AJAX 返回的 JSON 对象,key是我的 JSON 密钥的名称。

response.key.slice(1, response.key.length-1)

I used it like this with a regex replaceto preserve the line breaks and write the content of that key to a paragraph block in my HTML:

我像这样使用正则表达式replace来保留换行符并将该键的内容写入我的 HTML 中的段落块:

$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/\n/g, '<br/>'));

In this case, $('#description')is the paragraph tag I'm writing to. studyDatais my JSON object, and descriptionis my key with a multi-line value.

在这种情况下,$('#description')是我要写入的段落标记。studyData是我的 JSON 对象,description是我的具有多行值的键。

回答by killa-byte

Someone heresuggested using eval()to remove the quotes from a string. Don't do that, that's just begging for code injection.

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

Another way to do this that I don't see listed here is using:

我没有看到这里列出的另一种方法是使用:

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

回答by Joshua Michael Waggoner

What you are doing is making a JSON string in your example. Either don't use the JSON.stringify()or if you ever do have JSON data coming back and you don't want quotations, Simply use JSON.parse()to remove quotations around JSON responses! Don't use regex, there's no need to.

您正在做的是在您的示例中制作一个 JSON 字符串。要么不使用 ,JSON.stringify()或者如果您确实有 JSON 数据返回并且您不想要引用,只需使用JSON.parse()删除围绕 JSON 响应的引用即可!不要使用正则表达式,没有必要。

回答by Deep Singh

I dont think there is a need to replace any quotes, this is a perfectly formed JSON string, you just need to convert JSON string into object.This article perfectly explains the situation : Link

我认为不需要替换任何引号,这是一个完美的 JSON 字符串,您只需要将 JSON 字符串转换为对象即可。这篇文章完美地说明了情况:链接

Example :

例子 :

success: function (data) {

        // assuming that everything is correct and there is no exception being thrown
        // output string {"d":"{"username":"hi","email":"[email protected]","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":"[email protected]","password":"123"}"}
        // The required string : {"d":{username:"hi",email:"[email protected]",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":"[email protected]","password":"123"}"
        console.log(data);                     // output  : Object {d="{"username":"hi","email":"[email protected]","password":"123"}"}
        console.log(data.d);                   // output : {"username":"hi","email":"[email protected]","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:"[email protected]",password:"123"}" (correct)
        console.log(content.username);         // output : hi 
        var _name = content.username;
        alert(_name);                         // hi

}

回答by RuD3B0y

You can simple try String(); to remove the quotes.

你可以简单的试试 String(); 删除引号。

Refer the first example here: https://www.w3schools.com/jsref/jsref_string.asp

请参阅此处的第一个示例:https: //www.w3schools.com/jsref/jsref_string.asp

Thank me later.

晚点再谢我。

PS: TO MODs: don't mistaken me for digging the dead old question. I faced this issue today and I came across this post while searching for the answer and I'm just posting the answer.

PS:致 MOD:不要误以为我在挖掘死旧的问题。我今天遇到了这个问题,我在寻找答案时遇到了这篇文章,我只是发布了答案。