What's the difference between Javascript Object and JSON object

前端 未结 5 1444
南笙
南笙 2020-11-27 02:55

Can anyone tell me the difference between Javascript Object and JSON object with an example?

5条回答
  •  攒了一身酷
    2020-11-27 03:20

    JSON is a text representation of a javscript object. It is effectively an object literal in javascript notation (hence the name - JavaScript Object Notation => JSON).

    If you want to "compare" two object, convert the text to objects then compare keys and values.

    Some examples of objects to/from text:

    // Create obj using an object literal
    var obj = {key: 'value'};
    
    // Convert to text using JSON.stringify
    var text = JSON.stringify(obj);
    
    // Show the value of text
    alert( text ); // {"key":"value"}
    
    // Create a new object from text
    var newObj = JSON.parse(text); // javascript object
    
    // Show the text version of newObj
    alert(JSON.stringify(newObj));  // {"key":"value"}
    
    // Use text as code
    var newObj2 = eval('(' + text + ')');
    
    // It is indeed a string literal
    alert(JSON.stringify(newObj2));  // {"key":"value"}
    

    If you want to compare two objects, convert them from JSON to objects (if they are JSON in the first place) then do something like:

    function compareObjects(a, b) {
      var i, p, aProps = [], bProps = [];
    
      // Simple test first
      if (a === b) {
        return true;
      }
    
      // Get properties of a
      for (p in a) {
        if (a.hasOwnProperty(p)) {
          aProps.push(p);
        } 
      }
    
      // Get properties of b
      for (p in b ) {
        if (b.hasOwnProperty(p)) {
          bProps.push(p);
        } 
      }
    
      // If don't have same properties, return false
      if (aProps.sort().join('') != bProps.sort().join('')) {
        return false;
      }
    
      // If property values aren't the same, return false
      i = aProps.length;
      while (i--) {
        if (a[aProps[i]] !== b[bProps[i]]) {
          return false;
        }
      }
    
      // If passed all tests, must be equal
      return true;
    }
    

提交回复
热议问题