javascript - 如何在 Vue js 中解析 json 以在模板中使用它

标签 javascript json vue.js

<template>
    <div class="row w-20 " >
        <div class="card col-4 ">
             <ul>
                <li v-for="message in conversations.messages" :key="message.messages">
                    {{message.text}}  
                </li>        
            </ul> 
            
        </div>
    </div>
</template>

<script>
export default {
    
    props: ['conversation_index_route'],
    data() {
        return {
            conversations: [],
            url: 'http://127.0.0.1:8000/comms/conversation/',
            
        }
    },
    beforeMount() {
        this.$eventBus.$on('selectConversation', this.getConversations)
        this.getConversations();
    },
    methods: {
        getConversations(id) {
            console.log(this.url+id);
            axios.get(this.url+ id)
                .then(response => {
                    this.conversations = response.data;
                    this.conversations = JSON.parse(this.conversations.message);
                    console.log(this.conversations);

                });            
        }
    }

}
</script>
conversation_index_route:"http://127.0.0.1:8000/comms/conversation"
conversations:Object
all_staff_attended:false
centre_id:5
children:""
classes:""
cover_image:"https://via.placeholder.com/150x150"
created_at:"2020-05-30 19:01:59"
exited_users:null
id:257
last_id:null
messages:Array[1]
  0:"{"_id":1854,"text":"This is the beginning of this conversation","createdAt":"2020-05-30 19:01:59","system":true}"
parent_users:"3016"
parents:Array[1]
staff_users:"180,181"
staffs:Array[2]
status_id:1
title:"Test"
updated_at:"2020-05-30 19:01:59"
url:"http://127.0.0.1:8000/comms/conversation/"
This is the data I got and want to parse the message array
那么我应该编写什么代码来使用模板中的消息文本来显示文本消息?

最佳答案

你必须做 JSON.parse(conversations.messages[0]).text .这样你解析 messages 里面的对象并可以访问其属性。

关于javascript - 如何在 Vue js 中解析 json 以在模板中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62546770/

相关文章:

json - Lua - 如何传递一个API调用获得的字符串来调用另一个API调用

javascript - 使用 JavaScript 以编程方式创建 Json 文件

php - 如何解码youtube api v3 json响应

javascript - 使用应用程序脚本从 Google Sheets 读取内容并将内容写入 Google Doc

javascript - getJSON 在第 4 个元素后返回 'undefined'

javascript - 获取 v-model 值并使用 v-for 创建循环

vue.js - 我是否总是需要将组件内容包装在一个 div(或类似的)中?

php - this.$set() 在 vue2.0 和 laravel 中不工作

javascript - 使用 jQuery 匹配表格单元格中的日期模式

javascript - 如何使用jquery改变图像的颜色