AnApprentice
AnApprentice

Reputation: 110960

How to make a JSON object?

I want to create the following JSON object, as seen from a console log:

Object
    .   member: Object
    .   id: 8286

I've been trying:

'member' :[{'id': 8286}]

but get the following error: "Uncaught SyntaxError: Unexpected token :"

What am I doing wrong? Thanks

Upvotes: 3

Views: 6048

Answers (4)

user3374031
user3374031

Reputation: 1

{
   "member":{
      "id":value
   },
   {
      another_data:value
   }
}

the data are accesses using the(.) operator. the id is accessed by member.id

Upvotes: 0

Kai Qing
Kai Qing

Reputation: 18833

var member = {
    id: 8286
};

this allows you to access it like

member.id

You could have also meant something like:

var data = {
    member: {
        id: 8286
    }
};

which you would access like so:

data.member.id

If this is not what you want, please clarify in your post cause I'm not sure I'm following your request correctly.

Upvotes: 6

Dan Suceava
Dan Suceava

Reputation: 157

You're missing the curly braces surrounding the object. As in:

var x = {'member': [{'id':8286}]};

Upvotes: 3

fivedigit
fivedigit

Reputation: 18682

You mean something like this?

{
    "member": {},
    "id": 8286
}

Upvotes: 1

Related Questions