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 transfer javascript object to json

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to convert javascript object to json", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to convert javascript object to json!

The method of converting javascript object to json: 1. Convert JSON object to JSON character by "var last=obj.toJSONString()" method;2. Convert by "var last=JSON.stringify(obj);" method.

Operating environment of this article: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

How to convert javascript object to json?

JavaScript object and json conversion

JSON string:

var str1 = '{ "name": "cxh", "sex": "man" }';

JSON objects:

var str2 = { "name": "cxh", "sex": "man" };

1. Convert JSON string to JSON object

To use str1 above, you must first convert it to a JSON object using the following method:

var obj = eval ('(' + str + ')');//Convert JSON string to JSON object

or

var obj = str.parseJSON(); //Convert JSON string to JSON object

or

var obj = JSON.parse(str); //Convert JSON string to JSON object

Then you can read it like this:

Alert(obj.name);Alert(obj.sex);

Note that if obj is originally a JSON object, it will still be a JSON object after conversion (even if it is converted many times) using eval () function, but there will be problems after processing with parseJSON () function (throwing syntax exception).

2. You can convert a JSON object to a JSON string using toJSONString() or the global method JSON.stringify().

For example:

var last=obj.toJSONString(); //Convert JSON objects to JSON characters

or

var last=JSON.stringify(obj); //Convert JSON objects to JSON characters alert(last);

Note:

Of the above methods, except for the eval() function, which comes with js, the other methods come from the json.js package. The new version of JSON modifies the API by injecting both JSON.stringify() and JSON.parse() methods into Javascript built-in objects, the former becoming Object.toJSONString() and the latter becoming String.parseJSON(). If the toJSONString() and parseJSON() methods are not found, your json package is too old.

At this point, I believe that everyone has a deeper understanding of "how to convert javascript object to json", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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