[英]JSON parse returns empty string?
I am not sure if this is an issue about VueJS or JS itself.我不确定这是 VueJS 还是 JS 本身的问题。
I have string in my DB (converted JS Object with JSON.stringify() ) which looks like this:我的数据库中有字符串(使用 JSON.stringify() 转换的 JS 对象),如下所示:
{"type":5,"values":{"7":"/data/images/structured-content/64-7-scico.jpg","8":"<b>wefwe</b>","9":"Nějaký text","10":"/data/images/structured-content/64-10-scico.jpg"}}
What I wanted to do is select it from database (via Axios), convert it back to JS object and set it to VueJS data:我想要做的是从数据库中选择它(通过 Axios),将其转换回 JS 对象并将其设置为 VueJS 数据:
.then(response => {
if (response.data.response === "ok" && response.status == 200) {
// get data
let data = response.data.data.data[0];
// pass name to state
this.objectName = data.name;
// get json in string format
let result = data.content;
// first log
console.log(result);
// convert string to json
let content = JSON.parse( result );
// second log
console.log( content.values );
// update states
this.IDType = content.type;
this.values = content.values;
}
})
Axios , data.name and content.type works fine, however the second log ( content.values ) seems to be returning observer with empty strings which I can't pass to Vue data and work with it, as you can see on the screen below, values in this object are just empty no matter what I do. Axios 、 data.name和content.type工作正常,但是第二个日志( content.values )似乎正在返回带有空字符串的观察者,我无法将其传递给 Vue 数据并使用它,正如您在屏幕上看到的下面,无论我做什么,这个对象中的值都是空的。 What is exactly wrong in here?
这里到底有什么问题? Thank you!
谢谢!
from debugger:从调试器:
To be reactive Vue needs to know what the keys of an object are before you assign them.要成为响应式 Vue 需要在分配对象之前知道它们的键是什么。 So if they are known, pre assign them:
因此,如果它们是已知的,请预先分配它们:
data : () => ({
values : {
7 : "",
8 : "",
9 : "",
10 : ""
}
}),
Then use object assign to set them:然后使用对象分配来设置它们:
Object.assign(this.values, content.values)
If the keys are unknown / dynamic values you can set them to be reactive using the $set
method:如果键是未知/动态值,您可以使用
$set
方法将它们设置为响应式:
Object.keys(content.values).forEach(
function(key){
this.$set(this.value, key, content.values[key])
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.