简体   繁体   English

无法访问JSON对象中的单个数据:如何从JSON对象中将数据选出到VUE中

[英]Can't access individual data in JSON object: how do I single out data form a JSON object into VUE

I have searched all over stack and the internet for help but can't seem to find anything that helps with my issue. 我已经在堆栈和互联网上搜索了所有帮助,但似乎找不到任何可以解决我的问题的信息。

I am currently trying to read data from a JSON file stored in my Vue src folder. 我目前正在尝试从存储在Vue src文件夹中的JSON文件读取数据。 The file consists of three arrays but the array name has been created with dots. 该文件由三个数组组成,但数组名称已用点创建。 Since the file is local, I could go in and change the name of the array but eventually I will have Vue reading this information dynamically from a web server so I don't want to change anything as I won't be able to do that in the future. 由于该文件是本地文件,因此我可以进入并更改数组的名称,但是最终我将让Vue从Web服务器动态读取此信息,所以我不想更改任何内容,因为我无法做到这一点在将来。

I would like to parse only a few bits of information from the file but can't seem to single that information out. 我只想解析文件中的一些信息,但似乎无法将这些信息选出来。

Here is the JSON: 这是JSON:

 {
  "staging_nr_front_33.333.33.333 ": {
   "date": "2018-09-10 13:05:02",
  "webserver": "running",
  "memory": "79MB of 527MB available",
  "token_status": "present",
  "space": "5.3G of 16G used (34%) 11G available"
 },
 "staging_roll_444.444.444.44 ": {
  "date": "2018-09-10 13:02:44",
  "webserver": "running",
  "memory": "391MB of 993MB available",
  "token_status": "present",
  "space": "4.3G of 39G used (12%) 32G available"
},
"staging_nr_cont_55.555.555.555 ": {
"date": "2018-09-10 13:05:02",
"webserver": "running",
"memory": "94MB of 7302MB available",
"token_status": "present",
"space": "3.9G of 7.8G used (50%) 3.9G available"
}
}

And here is my Vue Script section: 这是我的Vue脚本部分:

 import roomIndex from '../serv.json';

  export default {
   name: 'HelloWorld',
   data() {
       return {
           roomIndex
       }
   },

And here I try to call it in my template section: 在这里,我尝试在模板部分中调用它:

<p v-for="dt in roomIndex">{{ dt.date }} </p>

As of right now when I use this format I can get all the dates in the JSON file just fine but if I write: 截至目前,当我使用这种格式时,我可以很好地获取JSON文件中的所有日期,但是如果我这样写:

  <p v-for="dt in roomIndex">{{dt[0].date }}</p>

trying to single out only the first date, I get this error: Error in render: "TypeError: Cannot read property 'date' of undefined" 尝试仅选择第一个日期,我得到此错误:渲染错误:“ TypeError:无法读取未定义的属性'date'

I have tried so many things like putting the array name within brackets like: 我已经尝试了很多事情,例如将数组名放在方括号内:

 <p v-for="dt in roomIndex">{{ dt.[staging_nr_front_33.333.33.333].date }}</p>

But that didn't work either. 但这也不起作用。

How can I only get the date object from the first array? 如何仅从第一个数组获取日期对象?

I am so new to Vue and JSON so please bear with me if this is an easy answer or my code is completely wacky... 我对Vue和JSON还是很陌生,所以如果这是一个简单的答案或我的代码完全古怪,请多多包涵...

Thanks for your help!!!! 谢谢你的帮助!!!!

As mentioned in another comment it looks like it is an object rather than an array which is why it's not responding the way I felt like it should. 正如在另一条评论中提到的那样,它看起来像是一个对象而不是数组,这就是为什么它没有像我应该的那样响应。 since I need to display the date and key of each server (but in separate parts of the page) I came up with this: 因为我需要显示每个服务器的日期和密钥(但在页面的不同部分),所以我想到了:

<div  v-for="(value, key, index) in roomIndex">
      <div class="box>
        <h2>{{ key }}</h2>
        <h1>Server Status</h1>
        <div  class="circle" :class="value.webserver"></div>
           <h3>Date</h3>
        </div>
       </div>
</div>

This gave me a box where the name of the server was outputted as well as the date and a coloured circle depending on the state of the server for each server. 这给了我一个方框,其中输出了服务器的名称以及日期和彩色圆圈,具体取决于每个服务器的服务器状态。 It seemed to solve my issues. 它似乎解决了我的问题。

Thanks for all of your help! 感谢您所有的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM