Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

JSON 解析與 XML 解析對比:結構、流程與適用場景

編輯:關於 Android 程式設計

JSON 和 XML 是移動端最常見的兩種數據交換格式,目前 JSON 的佔有率明顯更高。本文在 Android 環境下對兩者的解析方式做一組對照,幫助你在接口對接時做出合適選擇。

出發點:解析到底在解什麼

無論哪種格式,解析的目的都是拿到每一個具體的值。JSON 是鍵值對形式,XML 是標簽與值的形式,本質上都是「用鍵或標簽找到對應的值」。所以兩者都可以先準備一個雙列集合來承接結果。

兩種數據結構的區別

JSON 分三類:

  • 對象類型:用大括號包裹,屬性與值一一對應;
  • 數組類型:用中括號包裹,可看作多個對象的組合;
  • 混合類型:同一份數據裡既有對象也有數組,大括號是對象、中括號是數組。

XML 以標簽形式存儲,通過標簽名取值,層級關係由標簽嵌套表達。

Android 下的 JSON 解析

有兩條路:一是自己按數據結構分層解析,二是用開源框架。

自己寫時又分三種情況:對象解析最簡單,先建 JSON 對象,用 getXxx() 系列方法取值後存入集合;數組解析先通過 JsonArray 拿到各個對象,再走對象解析流程;綜合解析則是按結構順序交替使用兩種手段,直到取到目標數據。用框架時原理相同,只是要先導包,並寫一個方法類去調用。

讀取對象類型:

讀取數組類型:

讀取綜合類型:

框架方式的目錄結構:

XML 解析:XmlPullParser

XML 解析是「邊讀邊解析」,自上而下依次推進,流程相對固定:

  1. 創建 XmlPullParser 實例,設置待解析的流對象;
  2. 在循環中判斷當前事件類型,遇到 END_DOCUMENT 跳出;
  3. 若是 START_TAG,進入標簽內部,比對標簽名,命中則用 nextText() 取文本;
  4. 若是 END_TAG,把已取到的數據存入集合;
  5. 別忘了在循環末尾調用 type = parser.next() 把指針往後推。

完整示例:從 Tomcat 拉取並解析

下面的案例用 Android 設備從 Tomcat 服務器分別獲取 JSON 與 XML 數據,解析後展示在界面上。

JSON 原始數據:

{
  "resultcode": "200",
  "reason": "查詢成功!",
  "result": {
    "sk": { "temp": "21", "wind_direction": "西風", "wind_strength": "2級", "humidity": "4%", "time": "14:25" },
    "today": {
      "city": "天津", "date_y": "2014年03月21日", "week": "星期五",
      "temperature": "8℃~20℃", "weather": "晴轉霾",
      "weather_id": { "fa": "00", "fb": "53" },
      "wind": "西南風微風", "dressing_index": "較冷",
      "dressing_advice": "建議著大衣、呢外套加毛衣、衛衣等服裝。",
      "uv_index": "中等", "comfort_index": "", "wash_index": "較適宜",
      "travel_index": "適宜", "exercise_index": "較適宜", "drying_index": ""
    },
    "future": [
      { "temperature": "28℃~36℃", "weather": "晴轉多雲", "weather_id": {"fa":"00","fb":"01"}, "wind": "南風3-4級", "week": "星期一", "date": "20140804" },
      { "temperature": "28℃~36℃", "weather": "晴轉多雲", "weather_id": {"fa":"00","fb":"01"}, "wind": "東南風3-4級", "week": "星期二", "date": "20140805" },
      { "temperature": "27℃~35℃", "weather": "晴轉多雲", "weather_id": {"fa":"00","fb":"01"}, "wind": "東南風3-4級", "week": "星期三", "date": "20140806" },
      { "temperature": "27℃~34℃", "weather": "多雲", "weather_id": {"fa":"01","fb":"01"}, "wind": "東南風3-4級", "week": "星期四", "date": "20140807" },
      { "temperature": "27℃~33℃", "weather": "多雲", "weather_id": {"fa":"01","fb":"01"}, "wind": "東北風4-5級", "week": "星期五", "date": "20140808" },
      { "temperature": "26℃~33℃", "weather": "多雲", "weather_id": {"fa":"01","fb":"01"}, "wind": "北風4-5級", "week": "星期六", "date": "20140809" },
      { "temperature": "26℃~33℃", "weather": "多雲", "weather_id": {"fa":"01","fb":"01"}, "wind": "北風4-5級", "week": "星期日", "date": "20140810" }
    ]
  },
  "error_code": 0
}

解析其中的 future 數組,結果如下:

XML 原始數據:

<?xml version="1.0" encoding="UTF-8" ?>
<channel>
  <item>
    <title>新聞標題一</title>
    <description>新聞摘要內容一</description>
    <image>https://192.168.1.103:8080/img/a.jpg</image>
    <type>1</type>
    <comment>163</comment>
  </item>
  <item>
    <title>新聞標題二</title>
    <description>新聞摘要內容二</description>
    <image>https://192.168.1.103:8080/img/b.jpg</image>
    <type>2</type>
  </item>
  <item>
    <title>新聞標題三</title>
    <description>新聞摘要內容三</description>
    <image>https://192.168.1.103:8080/img/c.jpg</image>
    <type>3</type>
  </item>
  <item>
    <title>新聞標題四</title>
    <description>新聞摘要內容四</description>
    <image>https://192.168.1.103:8080/img/d.jpg</image>
    <type>1</type>
    <comment>763</comment>
  </item>
</channel>

解析後的成果展示:

該怎麼選

JSON 的優勢在於體積小、解析速度快、與前端 JavaScript 天然契合,寫起來也更省事,因此新接口基本首選 JSON。XML 的優勢在於結構嚴謹、命名空間與文檔規範完善,適合配置檔、RSS、SOAP 等需要強約定的場景。而在 Android 端,XML 解析走 XmlPullParser 是省內存的做法,它不需要像 DOM 那樣先把整棵樹load進內存。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved