Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Array in JSON

Tags:

java

json

I am working on a research project that requires storing a large amount of external data. I have settled on the JSON format, which I have never used, as a storage format. All I really need to store is a large array of data however every example of JSON I can find that has an array in it is nested inside of an object. for example:

{ "NumberList" : {
    "array" : [ 1, 2, 3, 4, 5, 6] }}

Is it possible to have only an array? Such as:

"array" : [1,2,3,4,5,6]

Without an opening and closing bracket or a surrounding object?

like image 859
retrohacker Avatar asked Oct 02 '12 16:10

retrohacker


1 Answers

The accepted answer is wrong. JSON can start and end with an array. The official JSON document says

JSON is built on two structures:

  1. 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.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

You can also see it through JSON validator.

In a nutshell, while you still cannot do "array" : [1,2,3,4,5,6], you can store it like [1,2,3,4,5,6].

like image 192
massanishi Avatar answered Oct 28 '22 00:10

massanishi