简体   繁体   English

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

[英]remove double quotes from Json return data using Jquery

I use JQuery to get Json data, but the data it display has double quotes.我用JQuery得到Json数据,但是它显示的数据有双引号。 It there a function to remove it?它有一个 function 删除它?

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

it returns:它返回:

"House"

How can I remove the double quote?如何删除双引号? Cheers.干杯。

Use replace :使用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表示“全局”(全部替换)。

For niche needs when you know your data like your example ... this works :当您像示例一样了解数据时,对于利基需求......这有效:

JSON.parse(this_is_double_quoted);

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

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. JSON 在加载时由 jQuery 解析,您无需解析数据即可使用它。 Just use the string in the data:只需使用数据中的字符串:

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

Someone here suggested using eval() to remove the quotes from a string. 这里有人建议使用eval()从字符串中删除引号。 Don't do that, that's just begging for code injection.不要那样做,那只是乞求代码注入。

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

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.我也有这个问题,但就我而言,我不想使用正则表达式,因为我的 JSON 值可能包含引号。 Hopefully my answer will help others in the future.希望我的回答能在未来对其他人有所帮助。

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.这对我JSON.stringify() ,因为我在生成它的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:我像这样使用正则表达式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.在这种情况下, $('#description')是我要写入的段落标记。 studyData is my JSON object, and description is my key with a multi-line value. studyData是我的 JSON 对象,而description是我的多行值的键。

What you are doing is making a JSON string in your example.您正在做的是在您的示例中制作一个 JSON 字符串。 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!要么不要使用JSON.stringify()或者如果您确实有 JSON 数据返回并且您不想要引用,只需使用JSON.parse()删除围绕 JSON 响应的引用! Don't use regex, there's no need to.不要使用正则表达式,没有必要。

You can simple try String();你可以简单的试试 String(); to remove the quotes.删除引号。

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. PS:致 MOD:不要误以为我在挖掘死旧的问题。 I faced this issue today and I came across this post while searching for the answer and I'm just posting the answer.我今天遇到了这个问题,我在寻找答案时遇到了这篇文章,我只是发布了答案。

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

}

I had similar situation in a Promise just solved doing a cast as (String)我在 Promise 中遇到了类似的情况,刚刚解决了将强制转换为 (String)

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

  return String (getRole?.id);
}

I used this Website and selected No quotes for Keys https://csvjson.com/json_beautifier ?我使用了这个网站并为 Keys https://csvjson.com/json_beautifier选择了无引号?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM