Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize string conversion object by vuejs

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "vuejs how to achieve string conversion object", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "vuejs how to convert strings to objects".

Vuejs to achieve string transfer object method: 1, read the original data from the database table; 2, through the "this.temporary=JSON.parse (row.selections);" method to convert the string into json object.

This article operating environment: windows7 system, vue2.9.6 version, DELL G3 computer.

How does vuejs implement string conversion objects?

Vue.js converts String type to json format and json object to String:

When writing a vue project, because of the large amount of data, the conversion format is relatively complex, so the direct form acquisition value is stored in the database table in String format, but when editing the record, it has to be read from the database table by id, and then displayed on the page accordingly.

So: teach you how to convert String format to Json format in vue project:

Let's look at the data format stored in the database; this paper takes the selections field as an example.

Next, I'm going to demonstrate how String format and json format are converted relative to each other.

Part of the important demonstration code for vue.js is attached:

EditFormBuilder (row) {/ / aa bb cc is just for debugging to see more clearly the data taken from the database table / / read the original data from the database table, pay attention to the data format console.log ("aa", row.selections); / / next, how to parse to json this.temporary=JSON.parse (row.selections) / / convert the string to the json object this.items=this.temporary; console.log ("bb", this.items); / / Note, convert the object to string this.flag=JSON.stringify (row.selections); / / convert the json object to the string this.items=this.flg; console.log ("cc", this.items); / / convert the format again! Pay attention to see}

Pay attention to the following picture! You will realize the charm of JSON.parse and JSON.stringify!

By the way, I would like to introduce the usage of the two functions in detail: [ps: for details, jump to the rookie tutorial to the rookie tutorial url]

(1) JSON.parse function:

Function: convert json strings into json object syntax: JSON. Parse (text [, reviver]). Parameter: text must be; a valid json string. Reviver: optional. Return value: an object or array.

(2) JSON.stringify () function:

Purpose: converts json objects into json strings. Syntax: JSON.stringify (value [, replacer] [, space]) parameter: value must; usually an object or array. Replacer: optional function or array used to convert the result. Space: optional. Add indents, spaces, and newline characters to the return value JSON text to make it easier to read. Return value: a string containing JSON text. At this point, I believe you have a deeper understanding of "vuejs how to achieve string conversion object", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report