You create a *.json file the way you create any text file. In a *.json can be declared any type of value that is accepted, such as null
, object expression {}
, expression of Array
[]
, numerical values, String
, Boolean
and null
, therefore, it cannot be vázio.
Differences:
- In an object you can only declare property names with quotes between them.
- if you want to escape characters, declare one to look like this: n (or
\\
, computably) in front of the character.
In Javascript, the object JSON
includes two methods:
* `parse`: Interpreta uma string como JSON.
* `stringify`: Transforma um objeto em string, no formato de JSON.
Role model:
var data = JSON.parse('{"version": 0.5}');
alert("Version: " + data.version);
// Exemplo; expandindo o objeto retornado
with(JSON.parse('{"a": "bla\\nbla", "b": true, "c": "ey"}'))
alert("a: " + a), // -> "bla\nbla"
alert("b: " + b), // -> true
alert("c: " + c); // -> "ey"
// Exemplo: inline
alert("a: " + JSON.parse('{"a": "inline"}').a) // -> "inline"
Remembering: Some old browsers may not support the object JSON
. This is not worrying, but if you want to detect, make a condition like this: typeof window.JSON === "object"
, or use some "polyfill" existing.
Advangioso? If you want a project to be easy to work with, JSON makes it easy to read and manipulate data. Disadvantage compared to comma separated data advantage (,
), which decreases the amount of characters, is insignificant. I would say it is advantageous.
The doubt in your comment is not related to JSON. You can index any string in an object and then index another object. This is normal in languages such as Lua, Javascript, etc:
var object = {index: {b: true}};
alert(object.index.a); // undefined
alert(object.index.b); // true
alert(object.b); // undefined
Can you give an example of this? I don’t understand the "call a link" part without JS?
– Laerte
I believe that "call a link", is because AJAX sends a request (POST, GET, PUT, DELETE...) to the address (so, "calling a link"). This address has the extension of
.json
. I understood it that way and I believe it to be that way. But responding... Probably the.json
is static or is changed periodically by another process (in PHP or not), thus creating and changing such file. This works as a cache, so a static content is always served, rather than having to take the data from a database. After all, isn’t it every day that you have a new city to select?! P– Inkeliz
I thank you for the help, regarding doubt is made a call via ajax yes but at the address that is in format . json instead of calling a file in . php format for example..
– Crazy