php 数组 转 json,JSON/PHP数组结构转换器

这段代码展示了如何使用JavaScript处理JSON和PHP数组之间的转换。它包含了一个计算属性`dst_array`,根据`selected`的值('json'或'php'),将源数组`src_array`从一种格式转换为另一种。转换过程涉及到字符串替换操作,以实现两种语法之间的等效。
摘要由CSDN通过智能技术生成

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

new Vue({

el: '#app',

data: {

selected: 'json',

src_array: '',

options: [{

text: 'JSON Array',

value: 'json'

}, {

text: 'PHP Array',

value: 'php'

}, ]

},

computed: {

// a computed getter

dst_array: function() {

if (this.src_array === "") {

return;

}

var cdata = '';

if (this.selected === 'php') {

var temp = this.src_array;

temp = temp.replace(/^\$[a-zA-Z0-9-_]+\s*=\s*\[/, '');

temp = temp.replace(/\[/g, '{');

temp = temp.replace(/]/g, '}');

temp = temp.replace(/\s*=>\s*"/g, ': "');

temp = temp.replace(/\s*=>\s*'/g, ': \'');

temp = temp.replace(/\s*=>\s*{'/g, ': {');

cdata = "[";

cdata += temp;

cdata += "]";

return cdata;

} else {

cdata = "public $params = [\n";

var temp = this.src_array;

if (temp.substr(0, 1) === "[" || temp.substr(0, 1) === "{") {

temp = temp.substr(1);

}

if (temp.substring(temp.length - 1, temp.length) === "]" ||

temp.substring(temp.length - 1, temp.length) === "}") {

temp = temp.substr(0, temp.length - 1).trim();

}

temp = temp.replace(/\{/g, '[');

temp = temp.replace(/\}/g, ']');

temp = temp.replace(/\:\s*"/g, ' => "');

temp = temp.replace(/\:\s*'/g, ' => \'');

temp = temp.replace(/\:\s*\[/g, ' => [');

cdata += temp;

cdata += "\n];";

return cdata;

}

}

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值