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

时间:2021-11-24 06:14:37

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.

如何删除双引号?干杯。

6 个解决方案

#1


57  

Use replace:

使用替换:

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

// "House"
// House

Note the g on the end means "global" (replace all).

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

#2


13  

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

当您知道您的数据时,如果您需要利基需求......这样可行:

JSON.parse(this_is_double_quoted);

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

#3


5  

The stringfy method 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:

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

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

#4


3  

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 textarea that produced it and as a result, I know that I'm always going to have the "s at each end of the string.

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

In this generalized example, response is the JSON object my AJAX returns, and key is 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 replace to preserve the line breaks and write the content of that key to a paragraph block in my HTML:

我使用正则表达式替换来保存换行符并将该键的内容写入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. studyData is my JSON object, and description is my key with a multi-line value.

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

#5


1  

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响应周围的引用!不要使用正则表达式,没有必要。

#6


0  

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":"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

}

#1


57  

Use replace:

使用替换:

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

// "House"
// House

Note the g on the end means "global" (replace all).

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

#2


13  

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

当您知道您的数据时,如果您需要利基需求......这样可行:

JSON.parse(this_is_double_quoted);

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

#3


5  

The stringfy method 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:

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

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

#4


3  

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 textarea that produced it and as a result, I know that I'm always going to have the "s at each end of the string.

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

In this generalized example, response is the JSON object my AJAX returns, and key is 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 replace to preserve the line breaks and write the content of that key to a paragraph block in my HTML:

我使用正则表达式替换来保存换行符并将该键的内容写入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. studyData is my JSON object, and description is my key with a multi-line value.

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

#5


1  

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响应周围的引用!不要使用正则表达式,没有必要。

#6


0  

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":"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

}