【问题标题】:remove double quotes from Json return data using Jquery使用 Jquery 从 Json 返回数据中删除双引号
【发布时间】:2013-05-16 22:22:20
【问题描述】:

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

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

它返回:

"House"

如何删除双引号?干杯。

【问题讨论】:

  • javascript替换功能应该可以工作

标签: javascript jquery


【解决方案1】:

使用replace:

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

// "House"
// House

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

【讨论】:

  • 太棒了!这正是我想要的。我替换了双引号,并尝试使用一些字符。好的!谢谢!!
  • 正则表达式虽然有效,但会造成大量内存浪费。我认为用户不应该使用 JSON.stringify(),或者如果它只是一个句子的 JSON 响应,使用 JSON.parse()。当事情扩大时,这些正则表达式会加起来。只是说。
  • 不修剪开始/结束引号。它会删除所有引号。
【解决方案2】:

当您知道您的数据(例如您的示例)时,满足利基需求......这很有效:

JSON.parse(this_is_double_quoted);

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

【讨论】:

    【解决方案3】:

    stringfy 方法不是用来解析 JSON 的,它是用来把一个对象变成一个 JSON 字符串的。

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

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

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

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

        在这个通用示例中,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 是我的具有多行值的键。

        【讨论】:

        • 这正是我正在研究的……非常有帮助,谢谢。 :)
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

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

          稍后谢谢我。

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

          【讨论】:

          • 完美!使用 String() 而不是 JSON.stringify()
          【解决方案8】:

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

          例子:

          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
          
          }
          

          【讨论】:

            【解决方案9】:

            我在 Promise 中遇到了类似的情况,刚刚解决了作为 (String) 的强制转换

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-03-05
              • 2015-07-15
              • 1970-01-01
              • 1970-01-01
              • 2013-09-09
              • 1970-01-01
              相关资源
              最近更新 更多