I just started to learn JSON :
which one of the following is correct :
var json = {"age":22} // my book writes like this
or
var json = {age:22} // internet show example like this
PHP storm generates
argument type number is not assignable to parameter type string
for both of them.
If the second one is correct show what is the difference with JS object then.
Well, you're using the term JSON, but the example you show isn't JSON. If you're talking about plain JavaScript objects, then both examples you gave are correct. When people refer to JSON, they're usually referring to the data type sent over client/server exchanges, which is very specific, and there are linters for that (see jsonlint, for example). PHPStorm's error is incorrect.
When working with JSON a validator/linter is an essential tool, especially dealing with larger sets of data.
Sending both of these through http://jsonlint.com/ the results are:
The first verifies as Valid JSON, the second reveals:
Parse error on line 1:
{age: 22}
-----^
Expecting 'STRING', '}'
Basically JSON are key value pairs , its basically object for storing data
The above scenarios mentioned are respect to your functionalites and usage of them
Both ways are valid in Javascript. But the quotes are needed in specific cases, like :
var obj = {
'foo bar': 0,
'foo-bar': 0,
'': 0
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With