SoFunction
Updated on 2025-03-02

Example of implementation method for converting JS objects and json strings

This article describes the implementation method of converting JS objects and json strings. Share it for your reference, as follows:

During the interview today, the interviewer asked the question of the mutual conversion between js objects and json strings. Since I didn’t care about it when using it, and I used it less at work, I didn’t answer it. So today I checked the materials and reviewed them to consolidate my knowledge in this area.

1. Convert js object to json string

When interacting with the backend, sometimes you need to convert the js object into a json string format. At this time, we need to reference it.This file, then call()method. For example:

var data = new Object();
var jsonData = (data);

2. Convert json string into js object

When getting backend data in Ajax at work, we get the json format, and sometimes we need to convert it into js object format. Here we use one of jQuery$.parseJSON()Method converts JSON format data into js object format. For example:

var jsonData = $.getJSON();
var data = $.parseJSON(jsonData);

Of course, it can also be used()Method, the same as above, but some browsers are()The support is not ideal, so try to use it when using it.parseJSON()method..parseJSON()Method will return to execution when supported by the browser()The result of the method, otherwise it will return a similar executioneval()The result of the method is obtained by referring to jQuery 1.9.1:

parseJSON: function( data ) {
 // Attempt to parse using the native JSON parser first
 if (  &&  ) {
  return ( data );
 }
 if ( data === null ) {
  return data;
 }
 if ( typeof data === "string" ) {
  // Make sure leading/trailing whitespace is removed (IE can't handle it)
  data = ( data );
  if ( data ) {
   // Make sure the incoming data is actual JSON
   // Logic borrowed from /
   if ( ( ( rvalidescape, "@" )
    .replace( rvalidtokens, "]" )
    .replace( rvalidbraces, "")) ) {
    return ( new Function( "return " + data ) )();
   }
  }
 }
 ( "Invalid JSON: " + data );
},

This completes the mutual transfer between the js object and the JSON string.

PS: Here are a few more practical json online tools for your reference:

OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json

JSONOnline formatting tools:
http://tools./code/jsonformat

Online XML/JSONConvert each other tools:
http://tools./code/xmljson

jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans

For more information about JavaScript, please view the special topic of this site: "Summary of json operation skills in JavaScript》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.