开发者

escaping " from array jquery

I have array like ["'address' is required", "'name-of-ceo' is required"] when i l开发者_开发问答oop through this array i am getting each letter. This may be happening due to ". Is there any way I can escape it and get ["'address' is required", "'name-of-ceo' is required"]?


It is better to find out who is making such a thing like

["'address' is required", "'name-of-ceo' is required"]

Rather fixing the faulted stuff.

I am positive that it is better to fix the source.


Please check this ,

 var myArray="["'address' is required", "'name-of-ceo' is required"]";
    myArray=myArray.replace(/"/gi,"\"");
    myArray=myArray.replace(/\[/gi,"");
    myArray=myArray.replace(/\]/gi,"");
    myArray=myArray.split(',');

    alert(myArray);


You need to replace "'s with " in place where you forming this array. On the server side, I think. This is not a javascript issue.


var string = "["'address' is required", "'name-of-ceo' is required"]";

string = string.replace(/"/g, '"');

If you want to parse this string into a Javascript object, You can use Douglas Crockford's JSON functions which can be downloaded from github here.

So, download json2.js from the above github page. Include it in your page like this:

<script src="json2.js"></script>

and then parse your JSON string using:

var myJsonObject = JSON.parse(string);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜