1、什麼是jsonjson
JSON(JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。JSON是JavaScript原生格式,這意味着在JavaScript中處理JSON數據不須要任何特殊的API或工具包。 數組
2、json的規則函數
lJSON的規則很簡單:對象是一個無序的「‘名稱/值’對」集合。一個對象以「{」(左括號)開始,「}」(右括號)結束。每一個「名稱」後跟一個「:」(冒號);「‘名稱/值’對」之間使用「,」(逗號)分隔。工具
規則以下:
1)映射用冒號(「:」)表示。名稱:值
2)並列的數據之間用逗號(「,」)分隔。名稱1:值1,名稱2:值2
3) 映射的集合(對象)用大括號(「{}」)表示。{名稱1:值1,名稱2:值2}
4) 並列數據的集合(數組)用方括號(「[]」)表示。
[
{名稱1:值,名稱2:值2},
{名稱1:值,名稱2:值2}
]
5 元素值可具備的類型:string, number, object, array, true, false, nullui
JSON 用冒號(而不是等號)來賦值。每一條賦值語句用逗號分開。整個對象用大括號封裝起來。可用大括號分級嵌套數據。
對象描述中存儲的數據能夠是字符串,數字或者布爾值。對象描述也可存儲函數,那就是對象的方法。spa
■ json格式其它解析案例code
例 子 一:
xml
<script language="JavaScript"> var people ={"firstName": "Brett", "lastName":"McLaughlin", "email":"}; alert(people.firstName); alert(people.lastName); alert(people.email); </script>
例子二:
對象
<script language="JavaScript"> var people ={ "programmers": [ {"firstName": "Brett", "email": "" }, {"firstName": "Jason", "email": "" } ] }; window.alert(people.programmers[0].firstName); window.alert(people.programmers[1].email); </script>
例子三:
ip
<script language="JavaScript"> var people ={ "programmers": [ { "firstName": "Brett", "email": }, { "firstName": "Jason", "email": "" }, { "firstName": "Elliotte", "lastName":"Harold", "email": " } ], "authors": [ { "firstName": "Isaac", "genre": "science fiction" }, { "firstName": "Tad", "genre": "fantasy" }, { "firstName": "Frank", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "instrument": "guitar" }, { "firstName": "Sergei", "instrument": "piano" } ]}; window.alert(people.programmers[1].firstName); window.alert(people.musicians[1].instrument); </script>
例子四
<script language="JavaScript"> var people ={ "username":"mary", "age":"20", "info":{"tel":"1234566","celltelphone":788666}, "address":[ {"city":"beijing","code":"1000022"}, {"city":"shanghai","code":"2210444"} ] }; window.alert(people.username); window.alert(people.info.tel); window.alert(people.address[0].city); </script>