简体   繁体   English

如何从javascript / Jquery中的默认字符串中删除双引号

[英]How to remove double quotes from default string in javascript/Jquery

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 想法很有帮助

Using below code you can remove double quotes from a string: 使用下面的代码,您可以从字符串中删除双引号:

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

It is solved by converting current object to string and then used eval function,Worked solved thanks. 它通过将当前对象转换为字符串然后使用eval函数解决,工作解决了谢谢。

var eventlist = JSON.stringify(eventresult.d);//Jsonresult

var eventstring = new String();

eventstring = eventlist.toString().replace(/"/g, "");

eval(eventstring );

Though you may resolved the issue, but question is about string you are retrieving. 虽然您可以解决问题,但问题是关于您正在检索的字符串。

its not JSON object in stringified format. 它不是字符串格式的JSON对象。 You says its comes from $.post then it should not be 你说它来自$ .post然后它不应该

{'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: 当你尝试在post调用中的任何变量中分配这个时,它只会将第一个字符串分配给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对象的有效String表示形式可以是:两个对象的数组

"[{'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)

Vipin不会尝试分配var a = eval(stringname)它会再次将其视为字符串使用eval函数直接我知道使用eval不是一个好主意,但有时它很方便它也只是一个示例服务器字符串是有效的字符串。

Do the Following to achieve it, 做以下事实来实现它,

1) put ur JSON.stringfy value in a VARIABLE , 1)将你的JSON.stringfy值放在VARIABLE中

eg, 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中

eg, JSON.parse(strLink); 例如, JSON.parse(strLink);

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

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