Document directory
- Example
- Nested JSON object definition
- Example of creating an object using the JSON Method -- try to edit
- Reference
What is JSON
JSON (JavaScript Object Notation) is a beautiful method for creating JavaScript objects.JSONIt is also a lightweight data exchange format. JSON is easy to read and write, and is conducive to machine parsing and generation. JSON is a better way to exchange data in XML instead of AJAX.
Monkey tip: The JSON definition method is similar to the direct definition method. The JSON definition method is to put the functions and attributes defined by the direct definition method in braces and remove the object names of the attributes and function signatures, change the equals sign to a colon, and change it to a comma after each line!
JSON format and syntax
var
jsonobject
=
{
//
Attribute syntax in the object (Attribute names and attribute values appear in pairs)
propertyname
:
value
,
//
Function syntax in the object (the function name and function content appear in pairs)
functionname
:
function
(
)
{
...;
}
};
- Jsonobject -- JSON Object Name
- Propertyname -- property name
- Functionname -- function name
- A pair of braces, including multiple "name/value" sets
- JSON is represented by a set of "name/value" pairs. It can also be understood as an Array)
- The attribute name or function name can be any string or even a null string (see the following example)
- Commas (,) are used to separate each pair of "name/value" pairs.
URL: http://www.dreamdu.com/javascript/json/
Example
var
site
=
{
URL
:
"
www.dreamdu.com
"
,
name
:
"
Mengzhidu
"
,
englishname
:
"
dreamdu
"
,
author
:
"
Cute monkey
"
,
summary
:
"
Free Webpage Design tutorial
"
,
pagescount
:
100
,
isOK
:
true
,
startdate
:
new
Date
(
2005
,
12
)
,
say
:
function
(
)
{
document
.
write
(
this
.
englishname
+
"
say : hello world!
"
)
}
,
age
:
function
(
)
{
document
.
write
(
this
.
name
+
"
Already
"
+
(
(
new
Date
(
)
.
getFullYear
(
)
)
-
this
.
startdate
.
getFullYear
(
)
)
+
"
Old!
"
)
}
};
The above is a typical JavaScript Object in JSON format. The object name is dreamdu, and each name and value use a colon:
Split. For example, the name author corresponds to the value monkey and the name age corresponds to the value 5.
Another example:
var
circle
=
{
x
:
6
,
y
:
8
,
r
:
5
}
;
The preceding definition defines an element with the x coordinate of 6, y coordinate of 8, and radius of 5.
Nested JSON object definition
var
sites
=
{
count
:
2
,
language
:
"
chinese
"
,
baidu
:
{
URL
:
"
www.baidu.com
"
,
name
:
"
Baidu
"
,
author
:
"
baidu
"
,
say
:
function
(
)
{
document
.
write
(
this
.
name
+
"
say hello
"
)
}
},
dreamdu
:
{
URL
:
"
www.dreamdu.com
"
,
name
:
"
Mengzhidu
"
,
author
:
"
monkey
"
,
say
:
function
(
)
{
document
.
write
(
this
.
name
+
"
say hello
"
)
}
}
};
The JSON representation of sites in the above example also contains two small JSON notation, so the JSON notation can be nested.
Example of creating an object using the JSON Method -- try to edit
- Create a JavaScript Object using the JSON Method
Additional reading
Reference
------------------------------------------------------------------------------
JSON is a lightweight data exchange format for ease of operation. Easy to read and write. It is also easy to parse and generate machines. For more information, see: http://www.json.org.
Most of the time, we need to assemble a string as a json object. First, we need to combine the string and then convert it into a json object. The following example shows how:
<Script type = "text/javascript">
<! --
Var a = 50, B = "xxx ";
Var arr = "{id:" + a + ", name: '" + B + "'}";
// -->
</Script>
Combined into the string arr, the next step is to convert it into an object, and soon we will think of using the eval method, but if this conversion will produce an error, today I will try it like this, how can we convert it into a json object? After being depressed for a long time, I found a solution in the json. js file provided on the json official website. The solution is as follows:
Add parentheses at both ends of the string and then the eval will be OK. The test code is as follows:
<Script type = "text/javascript">
<! --
Var a = 50, B = "xxx ";
Var arr = "{id:" + a + ", name: '" + B + "'}";
Arr = eval ('+ arr + ')')
Alert (arr. name );
// -->
</Script>
------------------------------------------------------------------------
// Added by zhangjun at 2011-03-01
Var xys = document. getElementById ("HiddenField1"). value;
Var polygonJson = eval ('+ "{\" rings \ ": [[" + xys + "]," + "\" spatialReference \": {\ "wkid \": 4326 }}" + ')');
// Convert the string to the json function eval. You must use this function to convert to json before using it. Although Javascript is of a weak type, you still need to convert it and add () brackets at both ends.
Var polygon = new esri. geometry. Polygon (polygonJson );
Var symbol = new esri. symbol. SimpleFillSymbol (esri. symbol. SimpleFillSymbol. STYLE_SOLID,
New esri. symbol. SimpleLineSymbol (esri. symbol. SimpleLineSymbol. STYLE_SOLID,
New dojo. Color ([255,255, 0]), 2), new dojo. Color ([255, 0, 0, 0.25]);
// [[Xys] The expression is incorrect.
// Var polygon = {"geometry": {"rings": [[xys], "spatialReference": {"wkid": 4326 }}, "symbol ": {"color": [0, 0, 0, 64], "outline": {"color": [0, 0, 0,255], "width": 1, "type": "esriSLS", "style": "esriSLSSolid"}, "type": "esriSFS", "style": "esriSFSSolid "}};
Var graphic = new esri. Graphic (polygon, symbol );
Map. graphics. clear ();
Map. graphics. add (graphic );