如何在数据库vue.js项目中存储嵌套对象数据。 (哪种格式?)

问题描述 投票:0回答:1

我有一个项目,我在其中创建嵌套对象中的数据,并将其存储在数据库中时,我正在执行JSON.stringify。一切正常,但是当我想从数据库中获取相同的数据时,我需要json.parse,但是在此之后,我将其分配给vuex状态,因此我从原始数据中丢失了一些嵌套数据。

数据库中的数据:

[{"id":"1-368","name":"\tSolan","days":1,"daynights":2,"hotel":{"hotel_data":[{"id":1,"title":"hotel 1"}],"checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":[{"id":1,"title":"hotel 1"}],"checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":""},{"id":"2-54","name":"Dharamsala","days":"3","daynights":4,"hotel":{"hotel_data":[{"id":3,"title":"hotel3"}],"checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":[{"id":2,"title":"hotel 2"}],"checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":""}]

分配给vuex状态后的数据:

[{"id":"1-368","name":"\tSolan","days":1,"daynights":2,"hotel":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":"","date_from":"08 Jan 2020","date_to":"09 Jan 2020"},{"id":"2-54","name":"Dharamsala","days":"3","daynights":4,"hotel":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":"","date_from":"09 Jan 2020","date_to":"12 Jan 2020"}]

在上面的代码中,您可以注意到hotel_data是一个嵌套数组,但是在将其分配给vuex状态后看不到。

代码:

const mutations = {

setItem(state,item){

state.item.tour_location=JSON.parse(item.tour_location);

}}

vue.js vuejs2 vue-component vuex vuetify.js
1个回答
0
投票

尝试将值的字符串化为JSON.parse(JSON.stringify(item.tour_location))换行

© www.soinside.com 2019 - 2024. All rights reserved.