[英]Make a Javascript array into JSON
I've tried searching google and nothing is clear, plus you guys are way faster and correct. 我已经尝试过搜索google,但尚不清楚,再加上你们都更快,更正确。
I have an array as follows: 我有一个数组如下:
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
How do I turn the bannertext
array into a JSON encoded array so that I can send it to a PHP page and break it down using the json_decode
function, and then how do I access the individual variables? 如何将bannertext
数组转换为JSON编码的数组,以便可以将其发送到PHP页面并使用json_decode
函数进行json_decode
,然后如何访问各个变量?
EDIT: Thanks to Gazler for that first part! 编辑:感谢盖兹勒的第一部分! My PHP page looks like this: 我的PHP页面如下所示:
$bannertext = $_GET['bannertext'];
$banner = json_decode($bannertext);
How do I access each of those strings now? 现在如何访问每个字符串? LIke echo $banner[1];
LIke echo $banner[1];
and so on? 等等?
You use JSON.stringify() however in IE you will need to include the json2 library as IE has no native JSON parsing. 您使用JSON.stringify(),但是在IE中您将需要包含json2库,因为IE没有本机JSON解析。
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
console.log(JSON.stringify(bannertext));
As an aside, you can instantiate an array using the array literal syntax. 另外,您可以使用数组文字语法实例化数组。
var bannertext = [];
Here is a blog post explaining the difference. 这是一篇解释差异的博客文章。
JSON.stringify: JSON.stringify:
JSON.stringify(bannertext);
Older browsers , IE7 and below, require an external library Json2 Free CDN IE7及更低版本的旧版浏览器需要外部库Json2 Free CDN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.