admin管理员组

文章数量:1287817

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

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

Share Improve this question asked Jan 6, 2012 at 21:26 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 9
  • bonsaiden.github./JavaScript-Garden – Zirak Commented Jan 6, 2012 at 21:29
  • 4 I don't upvote to pensate downvotes, but I should. This is a perfectly valid question. – Julio Santos 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. – Devin Burke Commented Jan 6, 2012 at 21:32
  • Try to you something like jshint. – jake 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? – Felix Kling Commented Jan 6, 2012 at 21:38
 |  Show 4 more ments

4 Answers 4

Reset to default 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.

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

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

You mean something like this?

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

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

本文标签: javascriptHow to make a JSON objectStack Overflow