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
}
})
})