views:

350

answers:

3

I'm trying to stringify a multi-array variable into a JSON string in Javascript. The

//i'm using functions from http://www.json.org/json2.js

var info = new Array(max);
for (var i=0; i<max; i++) {
  var coordinate = [25 , 32];
  info[i] = coordinate;
}
var result = JSON.stringify(info);

But result doesn't look like a JSON string at all. What am I doing wrong here?

+3  A: 

The result looks like this for me:

[[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32],[25,32]]

Which is fine as far as I can see. It might look a bit weird, but that is mostly because JSON is used a lot for objects, which have a slightly different notation. You can eval the string and get the array structure back though, so it looks fine to me.

ylebre
that looks like JSON to you?!?
annakata
JavaScript arrays are objects. However, JSON serializes them specially, just like they have their own literal syntax.
Matthew Flaschen
From json.org:An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).So yes, it looks like the correct JSON representation of a two-dimensional array to me.
ylebre
Seriously, this is not JSON - it's a valid *value* of a property within a JSON object maybe, but it's not valid JSON as written. You need to reread the specs.
annakata
I'm deeply confused now :(
Hectoret
According to the spec, how should a two-dimensional array with integers should look like in JSON then? I'm starting to wonder here.
ylebre
@annakata, this is clearly valid JSON. I'm not sure how anyone could disagree. Check it out at json.org (homepage)
Triptych
@annakata: this is valid JSON...
Paolo Bergantino
That's a JSON array, all right.
Nosredna
@ylebre - I think the confusion was that some didn't believe an array could be a top-level JSON-text (to use terminology from the RFC).
Triptych
@Triptych: that is what I figured - I also figured the rest of stackoverflow would help with the correct answer, which they did :)
ylebre
RFC says that an array is legal top-level JSON-text. http://www.ietf.org/rfc/rfc4627.txt?number=4627
Dustin Getz
A: 

That isn't a JSON string, its a JSON array. If you were to make that a valid JSON object, you would represent it like

{"info":[[25,32],[25,32],[25,32],[25,32]]}

This is equivalent to your array info

ryanday
A JSON array, in string format, is a valid JSON string.
Nosredna
+4  A: 

You, and many in this question, are confused about the JSON specification. The string you have is a valid JSON array.

From json.org

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Your example matches the second structure - the ordered list of values.

Also from json.org:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Doesn't leave much to the imagination. You've got a valid JSON array there. Have fun with it. But just to be annoyingly thorough:

From the RFC

RFC 4627, Section 2

2) JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

  JSON-text = object / array
Triptych