JavaScript objects and JSON strings are translated into each other

Source: Internet
Author: User

JSON is the main format of Internet Data transmission, Many programming languages have a parser and serializer for json, in the web foreground field, JS object and JSON string conversion requirements are more Common.

This article mainly explains the JS object and the JSON string conversion method and some details

first, If you need to use JSON transport in development and the corresponding transformations need to introduce json.js or JSON2.JS,:HTTPS://GITHUB.COM/DOUGLASCROCKFORD/JSON-JS in HTML

1. JS object to JSON string (serialized)

var jsontext = json.stringify (classtext);

JS object to the JSON string to call the Json.stringify () function, which is the most basic function when we do serialization, but if you also want to filter the results in the serialization process, after serialization format editing, what should be done?

1.1, serialization Process filtering data

There are two ways to filter data during serialization:

One is to pass in the second argument of the Stringify () function as shown in the following example, the array contains the key value that needs to be serialized, and the stringify () function only serializes the key values contained in the array, The key value that is not included is not serialized into the JSON string;

One is to pass in a function in the second parameter of the stringify () function, the function is to do different processing according to different key values, this way more custom, you can serialize the value of the array into a ', ' concatenation of the string, You can also modify any of the value values during serialization, or you can return a undefined that does not serialize a set of key-value pairs ... but it is important to note that the switch statement for this function must have default handling This function is generally a replacement (filter) Function.

1     varBook = {2Title: "JavaScript",3 authors:[4"mayun",5"mahuateng"6         ],7Edition:3,8year:2011,9     }Ten     varJsonText1 = json.stringify (book,["title", "edition"]);  one     varJSONTEXT2 = Json.stringify (book,function(key,value) { a         Switch(key) { -              case"authors": -                 returnValue.join (","); the              case"years": -                 return5000; -              case"edition": -                 returnundefined; +             default: -                 returnvalue; +         } a})

1.2, serialization Process editing format (here the editing format is mainly refers to the resulting JSON string Indentation)

The serialization process editing format is mainly implemented by the third parameter of the stringify () function, which is typically implemented in the following two ways:

    

JSONTEXT3 = json.stringify (book,null,10); jsonText4 = json.stringify (book,null, "---");

The first way is to pass in a number in the third argument n,n represents the number of spaces indented for each level, The example above is indented 10 spaces per level, n should be less than or equal to 10, if filling in a number greater than 10 stringify () function will be processed according to 10来;

The second way is to pass in a string in the third argument, which will be used as the indent character, and if the string length is greater than 10, only the first 10 characters will appear.

2. JSON string to JS object (parsing)

The JSON string converted to a JS object calls the Json.parse () function, and the only additional requirement we may have during the conversion process is filtering during the conversion process.

  

1     varJsonsource = ' {' title ': ' JavaScript ', ' authors ': [' mayun ', ' Mahuateng '], ' edition ': 3, ' year ': ', ' Date ': ' 2011-11-30t16:00:00.000z "}";2     varBookCopy1 =Json.parse (jsonsource);3     varBookCopy2 = Json.parse (jsonsource,function(key,value) {4         Switch(key) {5              case"date":6                 return NewDate (value);7              case"title":8                 returnOh;9             default:Ten                 returnvalue; one         } a})

Above two examples the first is a normal JSON string to the JS object, the second is added to the parsing process of custom parsing processing (used to handle this function is also known as a restore Function)

  

JavaScript objects and JSON strings are translated into each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.