Jump to content

JSON to create objects


retro-starr

Recommended Posts

JSON should have the ability to make objects with variable values like you can here w3schools object example 2. Though from looking at my example below, I can see how the computer could get confused; but there should be no reason it can't understand the first quotations in front of the colon is just the name, therefor it should treat the second quotations as the variable value.This would just be the JSON example of the link.

function person(firstname,lastname,age,eyecolor){ var this = {  "firstname":"firstname",  "lastname":"lastname",  "age":"age",  "eyecolor":"eyecolor", }}

Link to comment
Share on other sites

JSON should have the ability to make objects with variable values like you can here w3schools object example 2. Though from looking at my example below, I can see how the computer could get confused; but there should be no reason it can't understand the first quotations in front of the colon is just the name, therefor it should treat the second quotations as the variable value.This would just be the JSON example of the link.
function person(firstname,lastname,age,eyecolor){ var this = {  "firstname":"firstname",  "lastname":"lastname",  "age":"age",  "eyecolor":"eyecolor", }}

hmm, your links broken so I can't comment on its contents. There are a couple of things wrong with your example1) this is a reserved word, and can't be used for a variable name2) you were assigning string literals as variables, not the functions arguments3) you don't need a comma after the last member of an objectThere are a couple of ways to go about what you're trying to do.
function person(firstname, lastname, age, eyecolor){  this.firstname =  firstname;  this.lastname  = lastname;  this.age = age;  this.eyecolor = eyecolor;};var myFriend = new person("Bob", "Stevens", 23, "blue");alert("First name: " + myFriend.firstname);////OR////var myFriend ={  "firstname" : "Bob",  "lastname" : "Stevens",  "age": 23,  "eyecolor": "blue"};alert("First name: " + myFriend.firstname);

Most people would probably use the first method, it's more generic and flexible. Some might opt to use getter's and setters as opposed to initializing everything through the constructor.

Link to comment
Share on other sites

I'm never going to figure out how to post links properly, but here's what it should've said http://www.w3schools.com/js/tryit.asp?file..._create_object2Hehe, should've known about the second quotations. I changed myObject to person (or even myFather) so I could use it in the tryit program, but it just said "undefined is undefined years old". I think you should comma the last item as it doesn't hurt and it leaves room for expansion, might make a thread to hear what people say are best practices.

Link to comment
Share on other sites

Here's a variation that JSG posts a lot:

function ob (first, last, city, state) {	return {	  firstname: first,	  lastname: last,	  city: city,	  state: state   };}

He does it with methods, too, but at that point I find the syntax annoying to look at. :)

Link to comment
Share on other sites

I think you should comma the last item as it doesn't hurt and it leaves room for expansion.
Then it won't be valid JSON. If you exapand, then just add the comma then.
{	"fname" : "bob",	"lname" : "steven",	"age" : 23,	"eyecolor" : "blue",	}

try that here and you'll see. http://www.jsonlint.com/

Link to comment
Share on other sites

Remember JSON is a subset of the JavaScript language and you can do many things in JS that work but aren't valid JSON. For example, besides the trailing comma issue you can also use single quotes, whereas JSON only allows double-quotes.Edit: you can find normative syntax diagrams at http://json.org/.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...