JAVA解析JSON資料
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陣列
ps:JSONObject與JSONArray的區別
3.解析JSON資料(小編使用的GSON進行json資料的解析)
3-1 【JSONObject的解析】
下面是一個json檔案:
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
我們進行解析(解析一部分):
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.原始碼下載: