JSON 

JSON, which is an acronym for JavaScript Object Notation, is not a data type per se, but rather structured data. JSON is used as a lightweight data interchange format and is used by many programming languages. Not only will we cover this more a little later, but we will make extensive use of this format to pass data back and forth between our Angular application and the backend web services that we'll also be building for it. Just as programming languages have data types, data interchange formats often do as well. Here are the data types allowed to be represented in JSON:

  • String
  • Number
  • Object
  • Array
  • Boolean
  • Null

You may have noticed that there is a large overlap between JavaScript and JSON data types. This is not by accident because JSON is JavaScript Object Notation, and thus was modeled after JavaScript's data types. Here's an example of JSON data that contains the names and ages of three people (each of which is a JavaScript object):

{
“people”: [
{“name”:”Peter”, “age”:40},
{“name”:”Paul”, “age”:42},
{“name”:”Mary”, “age”:38}
]
}

In the previous JSON example, I have people as the key, and its value is an array of three people objects. There's no hard and fast rule that says you have to name structures nested structures, but it does become more legible. In this simple example, you could have instead omitted the key with no loss of data, as this next JSON sample shows:

[
{“name”:”Peter”, “age”:40},
{“name”:”Paul”, “age”:42},
{“name”:”Mary”, “age”:38}
]

However, the first example, where we have the people key, is not only easier to read but is also easier to work with in the code. When we write our RESTful web service APIs for our application in Chapter 12Integrating Backend Data Services, we will take the first approach, providing keys for our collections of data.

Here's an interesting note for you about data interchange formats. While there are a few formats to choose from, such as XML and SOAP (Simple Object Access Protocol), when developing web services, JSON is the most popular one of all, and it was inspired by JavaScript. 

Where would we be without JavaScript? I shudder to think about it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106