如何从 javascript/Jquery 中的默认字符串中删除双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28556648/
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
How to remove double quotes from default string in javascript/Jquery
提问by Ashwinik
I have one string
我有一根绳子
"{'name':'xyz'}","{'name':'PQR'}"
I need to remove double quotes it should be
我需要删除双引号它应该是
{'name':'xyz'},{'name':'PQR'}
I am able to remove double quotes but end result is always like below format
我能够删除双引号,但最终结果总是像下面的格式
"{'name':'xyz'},{'name':'PQR'}"
i want end result should be just
我希望最终结果应该只是
{'name':'xyz'},{'name':'PQR'}
Ideas are helpful
想法很有帮助
采纳答案by Ashwinik
It is solved by converting current object to string and then used evalfunction,Worked solved thanks.
它是通过将当前对象转换为字符串然后使用eval函数来解决的,工作解决了谢谢。
var eventlist = JSON.stringify(eventresult.d);//Jsonresult
var eventstring = new String();
eventstring = eventlist.toString().replace(/"/g, "");
eval(eventstring );
回答by Sunny
Using below code you can remove double quotes from a string:
使用以下代码,您可以从字符串中删除双引号:
var test = "\"House\"";
alert(test);
alert(test.replace(/\"/g, ""));
回答by vipul patel
Though you may resolved the issue, but question is about string you are retrieving.
虽然您可能解决了问题,但问题是关于您正在检索的字符串。
its not JSON object in stringified format. You says its comes from $.post then it should not be
它不是字符串化格式的 JSON 对象。你说它来自 $.post 那么它不应该是
{'name':'xyz'}","{'name':'PQR'}
{'name':'xyz'}","{'name':'PQR'}
Its not json string representation of any JS object.
它不是任何 JS 对象的 json 字符串表示。
When you try to assign this one in any variable in post call it will assign only first string to varibale:
当您尝试在调用后的任何变量中分配此变量时,它只会将第一个字符串分配给变量:
var a = {'name':'xyz'}","{'name':'PQR'}
See the result.
看看结果。
So first questing is is it valid string. Valid String representation of JS object could be : array of two object
所以第一个问题是它是有效的字符串。JS 对象的有效字符串表示可以是:两个对象的数组
"[{'name':'xyz'}","{'name':'PQR'}]"
Then just parse this string version of json object to get back JS object where
然后只需解析这个字符串版本的 json 对象即可返回 JS 对象,其中
JSON.parse(your string representation retrieved from server)
回答by Ash
Vipin don't try to assign var a =eval(stringname) it will again consider it as string use eval function directly i know using eval is not good idea but sometime it's handy also it was just an example server string was valid string.
Vipin 不会尝试分配 var a =eval(stringname) 它会再次将其视为字符串直接使用 eval 函数我知道使用 eval 不是一个好主意但有时它很方便而且它只是一个示例服务器字符串是有效字符串。
回答by Javed
Do the Following to achieve it,
执行以下操作以实现它,
1) put ur JSON.stringfy value in a VARIABLE,
1) 把你的JSON.stringfy 值放在一个 VARIABLE 中,
e.g, var strLink = JSON.stringify(offerObj[i].language[countData].cta);
例如, var strLink = JSON.stringify(offerObj[i].language[countData].cta);
2) Now put the VARIABLE in JSON.parse
2) 现在把VARIABLE 放在 JSON.parse 中
e.g, JSON.parse(strLink);
例如, JSON.parse(strLink);

