Storing an array in JSON

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

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

Is it possible to have only an array? For instance:

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

Without an opening and closing bracket or surrounding object?

+4
source share
3 answers

No, this is how JSON is formatted

Opening / closing { } say it is in json (I think kind of like <html></html> )

There is no reason you cannot do

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

This suggests that there is one field called an array that contains an array of numbers

+12
source

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

JSON is built on two structures:

  • A collection of name / value pairs. In different languages, this is implemented as an object, record, structure, dictionary, hash table, list of keys or associative array.
  • An ordered list of values. In most languages, this is implemented as an array, vector, list, or sequence.

You can also view it through a JSON validator .

In a nutshell, while you still cannot make an "array" : [1,2,3,4,5,6] , you can save it as [1,2,3,4,5,6] .

+4
source

Valid JSON always starts with { and ends with } . Take a look at http://json.org . SO must have {"array" : [1,2,3,4,5,6]}

+2
source

Source: https://habr.com/ru/post/1437423/


All Articles