2

I've lately read a lot about JSON and JavaScript object. But I become more and more confused, couse to me, they quite often look the same.

I have created an JavaScript class which I instantiate to an object like this:

function Person(Name, Lastname, Age, sGender)
{
    this.Name = Name;
    this.Lastname = Lastname;
    this.Age = Age;
    this.sGender = sGender;
}


var person = new Person(
    $('#PersonName').val(),
    $('#PersonLastName').val(),
    $('#PersonLastName').val(),
    $('#PersonAge').val(),
    $("#gender input[type='radio']:checked").val()
);

The #PersonName etc. are from my HTML input boxes. And at this part I know what it is that I'm doing.

But then I read about a simpler way of creating an object, so I gave it a go:

var oPerson = 
{
    "Name": $('#PersonName').val(),
    "Lastname": $('#PersonLastName').val(),
    "Age": $('#PersonAge').val(),
    "sGender": $("#gender input[type='radio']:checked").val()
}
aPeople.push(oPerson);
return oPerson;

But now I'm just really confused about what the code above is. Is this JSON or just a JavaScript object?

I hope that some of you bright minds can shed some light on this for me. :)

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144

3 Answers3

1

JSON = JavaScript Object Notation. That is, JSON is the text representation of a JavaScript object. The JSON notation happens to have the same syntax as JavaScript objects (or at least, a style of JavaScript objects).

Anytime you're creating an object in JavaScript (as in the code above), it's a JavaScript object. When you're transferring a JavaScript object as text, then you can use the JSON notation which is very natural in JavaScript. But you could also use an alternative format, such as XML or even a proprietary format.

But JSON is merely a representation of an object. When it's in code, it's an actual object.

Pete
  • 6,585
  • 5
  • 43
  • 69
0

The first example creates a class Person and instantiates it into a Person object. The second example just creates an object literal, which is just a plain Javascript object.

None of the above is JSON, which is a string representation of an array or object. For example, this is JSON:

var json = '{ "Name": "John", "LastName" : "Adnaves" }';

As you can see it very closely resembles a Javascript object but the key point is it's a string.

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

The second case is called Object Literal Notation, the syntax for it looks like this:

{
    propertyName: value;
    propertyName2: value,
    .
    .
    propertyNameN: value
}

The first case you mentioned creates a class for Person, or to be more exact: it is a constructor function used to create instances of Person.

It is useful when you want to create objects that inherit functionality and share common attributes with each other.

JSON *(as have @Pete said too) is Javascript Object Notation.

It's syntax is very similar to Object literals, but there are certain things which JSON can't represent (Object Literal Notation can do that) like functions, RegExps objects, infinity, NaN, undefined, etc ..

In other words, JSON is the string representation of an Object and it is used to transfer that object (mostly its properties, state if you will) across different computers.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95