JSON equals an associative array?

3

Are the two the same thing? Or is JSON a more complex library or technique?

    
asked by anonymous 20.11.2014 / 05:58

2 answers

5

JSON is a standard for writing / formatting content in a string . JSON means JavaScript Object Notation but in fact this format created by Douglas Crockford has been adapted by several languages and is now the standard format for sending content between applications and platforms.

So, like YAML and XML, JSON is pure text. The formatting rules are simple and the authority to check it is the link . Basically:

  • The data is written in key / value pairs
  • The data are separated by vigulas
  • To refer to objects, use {
  • To refer to arrays, use [
  • The quotation marks used are the double quotation marks "

So JSON is text and Arrays are language-specific and are dynamic representations of data with properties such as length , index, and others depending on the language.

Examples:

In JavaScript to declare an array is done

var arr = ['foo', 'bar'];

The same array in JSON would be a string:

var arrayJSON = '{ "arr": ["foo", "bar"]}';

Notice that following the JSON syntax, we always have to give a key for each value. And to use JavaScript you would have to convert with JSON.parse

var arr = JSON.parse(arrayJSON).arr;

To use the same array in JSON format in PHP would have to do:

$arr = json_decode('{ "arr": ["foo", "bar"]}', true);
    
20.11.2014 / 07:33
2

It has nothing to do. JSON is one thing and an associative array is something else.

JSON stands for Javascript Object Notation, and is a kind of "Serialized Array", ie you can generate it and carry it from place to place in string form, which makes it much easier to move data with this type of format. JSON is a subset of javascript and derivatives. ( Reference )

An associative array, depending on the language, is a kind of dictionary, it has keys that can be strings and associate them with values, for example:

[URL] => Array ( [Chave] => [Valor] );

The difference is in the manipulation of both, whereas JSON can be "converted" into a common string, the array is an object that needs to be serialized for data transport, so it is easier to use JSON in some In most cases, reading the array is much simpler, which makes maintenance and support easier. However, Array is widely used when you transpose values within the same application, because each language can have a different associative notation, unlike JSON, which is universal.

20.11.2014 / 06:08