admin 管理员组

文章数量: 1086019

I understand that JSON is for exchanging information. What's confusing me is, I would like to be able to use JSON for storing and calling Objects and their constructors if possible, but since JSON is literal notation I was wondering if there was a way to fill in the JSON object parameters with some type of constructor much like the way a normal constructor would work.

The closest thing I've found is this:

Normal Constructor:

var dude = function(name, age) {
this.name = name;
this.age = age;
}
var bro = new dude("chad", 22);

JSON:

var bro = {
'name': "chad", 
'age': 22
};

But even these aren't really the same considering with the constructor you can call var bro2 = new dude("tony", 21); at any time and have a new instance of dude whenever you want. How could you keep this type of functionality with JSON thrown into the mix?

I understand that JSON is for exchanging information. What's confusing me is, I would like to be able to use JSON for storing and calling Objects and their constructors if possible, but since JSON is literal notation I was wondering if there was a way to fill in the JSON object parameters with some type of constructor much like the way a normal constructor would work.

The closest thing I've found is this:

Normal Constructor:

var dude = function(name, age) {
this.name = name;
this.age = age;
}
var bro = new dude("chad", 22);

JSON:

var bro = {
'name': "chad", 
'age': 22
};

But even these aren't really the same considering with the constructor you can call var bro2 = new dude("tony", 21); at any time and have a new instance of dude whenever you want. How could you keep this type of functionality with JSON thrown into the mix?

Share Improve this question asked Jan 2, 2014 at 2:21 ryanhagzryanhagz 1931 gold badge2 silver badges15 bronze badges 9
  • JSON is a format for mainly strings, so your question isn't as easy to understand as you might think, are you really talking about just regular javascript objects ? – adeneo Commented Jan 2, 2014 at 2:23
  • Yes, but you can store plex objects in JSON as well. – ryanhagz Commented Jan 2, 2014 at 2:27
  • 2 What you are calling JSON in your second code example is not JSON. It is the creation of an object using JavaScript's literal syntax. JSON is a portable, platform-neutral data serialization scheme which uses a subset of JavaScript literal syntax. – JAAulde Commented Jan 2, 2014 at 2:28
  • I am not sure your JSON example is actually a JSON. JSON mean's javascript object notation, and is a string that can be easily serialised into a Javascript Object. – Yogesh Commented Jan 2, 2014 at 2:28
  • 1 Stop reading w3schools. They are notorious for bad advice, misnomers, etc. Further, they have no affiliation with the W3C and refuse to make that clear. – JAAulde Commented Jan 2, 2014 at 2:36
 |  Show 4 more ments

2 Answers 2

Reset to default 5

You have big misconception about what JSON really is.

This

var bro = {
  'name': "chad", 
  'age': 22
};

is javascript assignment operator with object literal. It is NOT JSON, however JSON representation of bro object would look surprisingly similar:

{ "name":"chad","age":22}

up to the tabs and spaces.

So real instantiation of object out of JSON in javascript would be

var bro = JSON.parse('{ "name":"chad","age":22}')

In order to better understand what is going on read this MDN article and this old John Resig's article on simple inheritance. Most likely it will answer your question

JSON is a very limited subset of JavaScript and can contain only data, not executable code. If you want to execute code for each object of certain types, you will have to manage that yourself.

One solution is to give each JSON object a type property:

// JSON data:
[{"type":"Book", "title"="Of Mice And Men", "author": { "type":"Dude", "name"="John Steinbeck" }}, ...]

and define "constructor" functions for each type:

var constructorsMap = {};
constructorsMap[ "Book" ] = function(o){ /* Book constructor code */ }
constructorsMap[ "Dude" ] = function(o){ /* Dude constructor code */ }

Then scan through each object after parsing it, to apply the "constructor" function after the fact:

var books = JSON.parse( data );// the book data above
applyConstructor( books );

Where applyConstructor is defined something like:

function applyConstructor(o)
{
   for( var p in o )
   {
     if( typeof o[p] == "object" )
     {
        applyConstructor(o[p]);
     }
     if( o.hasOwnProperty("type") )
     {
        if( typeof constructorsMap[o.type] == "function" )
        {
           constructorsMap[ o.type ].call( o, constructorsMap[o.type] );
        }
     }
   }
}

本文标签: javascriptCan you create a JSON constructorStack Overflow