ArticleDirectory
- What is JSON?
- Create a JSON Dataset
- Path and sub-Path
- Use the dataset Function
JSON is based on JavaScript Object markup. It is a lightweight data exchange format written in JavaScript. Simple to use and small files are preferred for some developers.
Now spry can use this data format on the page.
The spry region does not care about the data source. Therefore, you do not need to change the region and data reference when using a JSON dataset. JSON works in a similar way as an XML dataset, such as filtering and caching. Therefore, we will not discuss it in this article. For Basic Dataset functions, see Data Set overview.
The example uses JSON or JSON in two ways.
This article will discuss the differences between JSON and dataset features.
What is JSON?
JSON, similar to XML, uses a repeated data structure to transmit data. The following is a simple example:
{
"Firstname": "Jason ",
"Lastname": "Jones ",
"Address ":{
"City": "San Francisco, CA ",
& Quot; zip & quot;: 94121,
"Address": "444 Columbus Ave"
},
"Email ":[
"Jason@sf.com ",
Sjones@adobe.com"
]
}
For more information about JSON, see JSON entry. JSON itself allows the combination of nested structures. (That's why we should talk about the nested dataset solution later)
Create a JSON Dataset
There is only a small difference between the use of JSON and the use of XML datasets.
First, you need 'spryjsondataset. js'. 'sprydata. js', which includes region operations.CodeFiltering and other functions are required. 'xpath. js' is not required for JSON datasets '.
<SCRIPT src = "sprydata. JS "Language =" JavaScript "type =" text/JavaScript "> </SCRIPT> <SCRIPT src =" spryjsondataset. JS "Language =" JavaScript "type =" text/JavaScript "> </SCRIPT>
For a dataset, you must specify a file containing JSON data and a data node as the starting point of the dataset.
A typical JSON dataset structure is as follows:
VaR dsexample4 = new spry. Data. jsondataset (".../../data/JSON/object-02.js", {path: "batters. Batter "});
For JSON files, see here.
This structure starts with a variable name. This variable name is the name of the dataset. This name can be any legal name you want. The names of multiple datasets must be unique.
VaR dsexample4
Create a new object:
- New spry. Data. jsondataset
Note that JavaScript is case sensitive.
Next, specify the file path containing JSON information:
- New spry. Data. jsondataset ("../Data/JSON/object-02.js");
If it is a path to a file, wrap it in quotes as shown. Variables and Spry Data references can also be used here, without quotes.
The minimum procedure for creating a dataset.
Path and sub-Path
If you specify a part in the JSON data, you can specify a path. This is an optional parameter and needs to be included in.
- {Path: "node. sub node "}
Note:JSON uses vertices to indicate the path. Just like the path indicated by '/' in XML and XPath.
Another example:
{
"Firstname": "Jason ",
"Lastname": "Jones ",
"Address ":{
"City": "San Francisco, CA ",
& Quot; zip & quot;: 94121,
"Address": "444 Columbus Ave"
},
"Email ":[
"Jason@sf.com ",
Sjones@adobe.com"
]
}
If you want to obtain the "city" node in the file, write as follows:
VaR ds1 = new spry. Data. jsondataset ("myfile. js", {path: "address. City "});
Sub-Path
Because some JSON files are complex, the JSON dataset is flattening in another advanced mode.
Some JSON formats use nested structures, such as the following "image" and "thumbnail" attributes:
{"ID": "0001", "type": "Donut", "name": "cake", "image": {"url": "images/0001.jpg ", "width": 200, "height": 200}, "thumbnail": {"url": "images/thumbnails/0001.jpg"," width ": 32," height ": 32 }}
Sometimes we want the structure in the dataset to be flatten like a data column. When the top-level JSON object is flatten, you can use the "subpaths" option to tell the JSON dataset to contain nested structures, or use the "path" option to specify data. In the example, because the "path" option is not specified, the JSON dataset tries to flat only top-level objects. Contains the desired "image" nested knot. We only need to specify "image ".
VaR dsexample6 = new spry. Data. jsondataset (".../../data/JSON/object-03.js", {subpaths: "image "});
Spry extracts the "image" Node information and pulls it to the top-level node. Therefore, this data set contains the subnode information. Now, the "image" subnode can use 'image. 'Add the subnode name for reference.
{ID} |
{Type} |
{Name} |
{Image. Width} |
{Image. Height} |
{Image. url} |
Spry supports multiple sub-paths. In the preceding example, the 'thumbnail 'information may be used. You can specify the 'thumbnail' sub-path to extract it to the dataset.
VaR dsexample7 = new spry. Data. jsondataset (".../../data/JSON/object-03.js", {subpaths: ["image", "thumbnail"]});
Note that multiple sub-paths are actually a string array, which is included in []. Now the data reference looks like this:
{ID} |
{Type} |
{Name} |
{Image. Width} |
{Image. Height} |
{Image. url} |
{Thumbnail. Width} |
{Thumbnail. Height} |
{Thumbnail. url} |
Use the dataset Function
As a spry dataset, the JSON dataset has all the Basic Dataset functions.
For example, disable the cache and use unique (distinct ):
VaR ds1 = new spry. Data. jsondataset ("myfile. js", {path: "address. City", usecache: false, distinctonload: true });
One of spry's goals is that the region is independent of the dataset type. Areas can be exchanged between JSON, XML, and HTML datasets.
Copyright 2007. Adobe Systems inconfigurated. All rights reserved.
Localization: booolee.cnblogs.com