问题描述
我正在尝试对从 Swift 中的 JSON api 接收到的数据进行建模。数据如下所示:
<template>
<header class="hero">
<div class="container">
<div class="content">
<div class="textBox">
<transition name="fade" mode="out-in">
<template v-for="(slide,i) in slides">
<div class="slide-content" :key="slide.id" v-if="current == i">
<h1 class="punchline">
{{ slide.title }}
</h1>
<p class="caption">{{ slide.caption }}</p>
</div>
</template>
</transition>
<NuxtLink class="link" to="/">
<div class="icon-wrapper">
<Icon icon="chevron-right" />
</div>
</NuxtLink>
</div>
<div class="indicator">
<span
v-for="(slide,i) in slides"
:key="slide.id"
:class="{ active: current == i }"
class="item"
@click="jump(i)"
/>
</div>
</div>
</div>
<!-- this is the overlay -->
<Vector class="overlay" :class="color" file="hero-overlay" />
</header>
</template>
<script>
export default {
data() {
return {
current: 0,slides: [
{
id: 1,title: 'Slide 001',caption: 'say something',},{
id: 2,title: 'Slide 002',{
id: 3,title: 'Slide 003',],}
},computed: {
color() {
if (this.current == 1) return 'blue'
if (this.current == 2) return 'purple'
return 'default'
},methods: {
jump(index) {
this.current = index
}
}
}
</script>
<style scoped>
.overlay::v-deep svg path {
transition: fill 1s ease-out;
/* transition: all 1s ease-out; doesn't work either */
}
.overlay.default ::v-deep svg path {
fill: url(#primary);
}
.overlay.blue ::v-deep svg path {
fill: url(#blue);
}
.overlay.purple ::v-deep svg path {
fill: url(#purple);
}
</style>
如您所见,数据看起来有点奇怪,键中有许多空格、数字和特殊字符。现在我对 Swift 有点陌生,我一直在寻找对这些数据建模的最佳方法,以便我解析它并将其放入一个结构体中并用它做其他事情。
我的第一个反应是尝试将其放入一个结构体中,以便我可以将其传递给我的组合发布者中的 {
"Some Data": {
"1. information": "Some information goes here","2. Something": "Here",// Many more...
},"Time Series (5min)": {
"2021-02-03 20:00:00": {
"1. some": "89.0600","2. random": "89.3500","3. name": "88.8800","4. goes": "88.8800","5. here": "25858"
},"2021-02-03 13:35:00": {
"1. some": "89.0600",// Many more...
}
.decode(type: SomeData.self,decoder: JSONDecoder())
但显然键名意味着我不能使用简单的结构。我可能只是遍历所有内容并将每个键转换为我手动管理的新结构,但我相信有更好的方法!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)