Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 685
AnApprentice Avatar asked Mar 19 '26 03:03

AnApprentice


2 Answers

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.

like image 102
Kai Qing Avatar answered Mar 21 '26 17:03

Kai Qing


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

var x = {'member': [{'id':8286}]};
like image 29
Dan Suceava Avatar answered Mar 21 '26 15:03

Dan Suceava