Understand the difference between JOSN.parse() and JSON.stringify()
and solve the [object, object] problem
-
JOSN.parse(): used to
JSON 字符串
convert to对象
-
JSON.stringify(): used to
对象/数组
convert toJSON 字符串
When receiving data, [object, object] appears in printing because we originally passed an object, and the object was converted into a string during the transmission process. [object, object] is the string form of the object. This can usually be parsed using JSON.stringify().
-
For example: When using route jump to pass parameters in uniapp, when we pass an object;
You can use JSON.stringify when transferring to the jump page, first convert the data into JSON format;
Then use JSON.parse on the receiving page to parse the data into objects.
// 页面 A 跳转传参 let obj={ index: this.index, value: value } uni.navigateTo({ url: `/pages/A/A?obj=${ JSON.stringify(obj)}` })
// 页面 B 接收 onLoad(options) { console.log(JSON.parse(options.obj)) }