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?
- 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
2 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - Can you create a JSON constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744075062a2529196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论