简体   繁体   中英

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.

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. You says its comes from $.post then it should not be

{'name':'xyz'}","{'name':'PQR'}

Its not json string representation of any JS object.

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

"[{'name':'xyz'}","{'name':'PQR'}]"

Then just parse this string version of json object to get back JS object where

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 ,

eg, var strLink = JSON.stringify(offerObj[i].language[countData].cta);

2) Now put the VARIABLE in JSON.parse

eg, JSON.parse(strLink);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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