json:
{
"success": 1,
"data": [
{
"data": {
"code": "1,2,3,4,5",
},
"type": "demo1",
},
{
"data": {
"code": "2,3,4,5,6",
},
"type": "demo2",
}
]
}
JS代码:
onLoad:function(){
var that = this;
wx.request({
url: 'http://www.xxx.com/api.php?act=2',
header: {
'content-type': 'application/json'
},
success: function(res) {
that.setData({
codes:res.data.data[0].data.opencode.split(","),
}),
}
})
}
//如上,我要怎么才能把json里面的code变成数组,data[]里面的我不能写死。不然前面view输出一堆重复的,找了一天了没找到答案。
先谢谢大佬们了,给个思路,刚学小程序,很多不懂。
我想大声告诉你2017-05-19 10:15:22
完成了,经过各种尝试2333
html:
<text class="numberli" wx:for="{{opencode[index]}}" wx:key="opencode">{{item}}</text>
js:
success: function(res) {
var arrs = [];
res.data.data.map(function(item){
arrs.push(item.data.opencode.split(','))
}),
that.setData({
opencode:arrs,
}),
console.log(arrs);
}
为情所困2017-05-19 10:15:22
你最终想得到的是不是这样一个数据结构:
[
{
"data": {
"code": ["1", "2", "3", "4", "5"]
},
"type": "demo1"
},
{
"data": {
"code": ["2", "3", "4", "5", "6"]
},
"type": "demo2"
}
]
如果是的话,可以用下面的代码试试:
that.setData({
codes: res.data.map(item => {
return {
data: {
code: item.data.code.split(',')
},
type: item.type
}
})
})