1.什麼是JSON? (http://www.json.org/)
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
2.Json數據類型
2-1.json對象
2-2.json數組
3.解析JSON數據(小編使用的GSON進行json數據的解析)
3-1 【JSONObject的解析】
下面是一段json數據:
{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "24",
"wind_direction": "西南風",
"wind_strength": "2級",
"humidity": "51%",
"time": "10:11"
},
"today": {
"temperature": "16℃~27℃",
"weather": "陰轉多雲",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南風3-4 級",
"week": "星期四",
},
"future": [
{
"temperature": "16℃~27℃",
"weather": "陰轉多雲",
"weather_id": {
"fa": "02",
"fb": "01"
},
"wind": "西南風3-4 級",
"week": "星期四",
"date": "20150604"
},
]
},
"error_code": 0
}
我們進行解析(解析一部分):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
3-2 【JSONArray的解析】
下面是一個json文件
{
"cat":"it",
"language":[
{"id":1,"ide":"eclipse","name":Java},
{"id":2,"ide":"XCode","name":"Swift"},
{"id":3,"ide":"Visual Stdio","name":"C#"}
],
"pop":true
}
解析:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
3-3 【分析】
我們通過Gson進行解析,所以在使用前需要導入Gson.jar
解析json數據時,
1.需要進行創建Gson解析器
2.創建JSONObject對象
3.將json數據轉爲爲相應的數據
4.源代碼下載:
https://github.com/monsterLin/TestReadJSON
5.轉載: