【发布时间】:2019-12-31 01:05:37
【问题描述】:
我正在尝试从一些混乱的 JSON 构建一堆 Vue 对象。我可以为每种类型的属性创建一个干净的数组,但我不知道如何获取这些数组并将它们映射到 Vue 对象。
试图坚持使用 ES6。 jsfiddle:https://jsfiddle.net/rachidmrad/rfuLxgpz/1/
HTML
<div class="card w-50" v-for="brewery in breweries">
<div class="card-body">
<h5 class="card-title">Brewery: {{ brewery.name }}</h5>
<p class="card-text">
Location: {{ brewery.location }}<br />
Specialities: {{ brewery.speciality }}<br />
Tier: {{ brewery.tier }}
</p>
</div>
</div>
JS
data() {
return {
breweries:
{
name: [],
location: [],
speciality: [],
tier: []
}
}
},
mounted() {
let thisBrewery = this.name,
thisLocation = this.location,
thisSpeciality = this.speciality,
thisTier = this.tier;
// Get values from JSON - pretty sure I go this part right
get('https://spreadsheets.google.com/feeds/cells/1CTlh5HuhQd44_bTuQF_X02QzJpa0ReQJB5jb8MSFvO8/od6/public/values?alt=json')
.then((data) => {
let json = JSON.parse(data);
maxEntry = json.feed.entry.length /4 - 1;
for (let i = 0; i < maxEntry; i++) {
thisBrewery[i] = json.feed.entry[j].content.$t;
thisLocation[i] = json.feed.entry[j + 1].content.$t;
thisSpeciality[i] = json.feed.entry[j + 2].content.$t;
thisTier[i] = json.feed.entry[j + 3].content.$t;
j = j + 4;
}
})
.catch((err) => {
console.log('oops');
});
}
我基本上需要将 thisBrewery 映射到 breweries.name,thisLocation 映射到 breweries.location 等。
我不相信 JSON 在这里是相关的(我在非 Vue 应用程序中测试了我正在计算的数组并且他们检查了)但可以随意美化并查看数据:https://spreadsheets.google.com/feeds/cells/1CTlh5HuhQd44_bTuQF_X02QzJpa0ReQJB5jb8MSFvO8/od6/public/values?alt=json(这是Google 表格 JSON)
【问题讨论】:
标签: javascript json vue.js ecmascript-6 vuejs2