File in JSON format

Asked

Viewed 448 times

4

And oh guys all beauty?

I’m having a rather silly question, but I couldn’t find an answer. I’ve been going through some websites and I saw that instead of them calling a request $.AJAX on the server to populate a dropdown for example, they call a link in format JSON.

For example:

meulocal.com.br/cidades.json 

How can I create this file? It is advantageous to use this way?

I appreciate the help.

  • Can you give an example of this? I don’t understand the "call a link" part without JS?

  • 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

  • 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..

1 answer

4

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
  • I understood, I have a doubt in the following case, I make a file. JSON, I will have a value : { MT : {[1: 'AF', 2: 'CA']}, RS : {[12: 'TT']} } ?

  • @Crazy Easy! Just index "MT" in your JSON object. Ex: JSON.parse('{ "MT" : {"1": "AF", "2": "CA"}, "RS" : {"12": "TT"} }').MT... and you can index any property on that object, until it doesn’t exist. Run it on your console and you’ll see.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.