3

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

9
  • bonsaiden.github.com/JavaScript-Garden Commented Jan 6, 2012 at 21:29
  • 4
    I don't upvote to compensate downvotes, but I should. This is a perfectly valid question. Commented Jan 6, 2012 at 21:29
  • 1
    @JúlioSantos, that's a good rule, though. You might think the question deserves to be at zero, but you are giving the requestor +8 rep. In this case, that's probably fine, but in general it often rewards bad or mediocre questions. Commented Jan 6, 2012 at 21:32
  • Try to you something like jshint.com Commented Jan 6, 2012 at 21:32
  • 3
    Do you want to create JSON or a JavaScript object? Have you read a JavaScript Guide at all? Commented Jan 6, 2012 at 21:38

4 Answers 4

6
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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

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

2 Comments

It's not JSON, but OP is probably meaning a JavaScript object.
@FelixKling who's OP?
1

You mean something like this?

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

2 Comments

thats kind of what i thought but i think id needs to be in member to make sense... so obj->member->id
Then you mean var obj = {"member":{"id":8286}}, which can then be accessed with obj.member.id
0
{
   "member":{
      "id":value
   },
   {
      another_data:value
   }
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.