2

I was looking at my database and there were places where jQuery had been used to make a JSON string:

{"0":"33"}

And then I saw places where there were JSON strings made from PHP json_encode like:

["News"," world news"," latest news"]

Do the brackets versus braces make a difference?

1
  • Where's the jQuery here? Commented Apr 5, 2011 at 15:30

4 Answers 4

4

[] are used to create a simple array where {} creates an "object" used like an associative array in this case.

The first example assign the value 33 to the index 0, but you can use anything as an index. In the second example, you're creating a number indexed array.

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

2 Comments

Javascript does not have associative arrays. The first one is an object.
@Tomalak strictly speaking, you can't call it an object also. But object is probably better than hashmap or associative array.
3
{"0":"33"}

This is a literal that represents an Object with one element, whose key is "0" and whose answer is "33".

["News"," world news"," latest news"]

This is a literal that represents an Array with three elements, whose values are plain to see in the code.

jQuery has nothing to do with it. This is Javascript syntax, which is why JSON stands for JavaScript Object Notation.

Comments

2

The first one is an associative array (key-value pair, 0 is the key, 33 is the value). The second example is a list with 3 positions. They have both been "stringified" with JSON but are different data structures.

5 Comments

Javascript does not have associative arrays. The first one is an object.
From wikipedia: An associative array (also associative container, map, mapping, dictionary, finite map, and in query-processing an index or index file) is an abstract data type composed of a collection of unique keys and a collection of values. Even if it's not the official Javascript name, it is, data-structure wise, an associative array.
But that definition doesn't strictly map to Javascript Objects. Objects sort of emulate the concept of associative arrays, but they are not the same.
@Tomalak, do point me some references (even if JS internals) for me to learn where they differ :)
Actually I take it back; I still think of Objects in Javascript as far more than associative arrays, but having looked around I can see why you might call them that.
0

as said above, the difference in json is:

[ ] is array, accepts only value

{ } is object, accepts both key and value

They can be composited together as below:

var contact = {
     "Name": "John Doe",
     "PermissionToCall": true,
     "PhoneNumbers": [ 
       {
           "Location": "Home",
           "Number": "555-555-1234"
       },
       {
           "Location": "Work",
           "Number": "555-555-9999 Ext. 123"
       }
     ]
};

so next time you can figure out how to compose it yourself :D

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.