Android Studio - Complete Android Apps Tutorial - Parsing JSON & XML - Day 15 - 10.01
Parsing JSON & XML 2 Parse the XML input stream with XMLPullParser XmlPullParser xpp = Xml. newPullParser ; xpp.setInputnew InputStreamReaderis; boolean start = false; int eventType = xpp.getEventType; whileeventType!= XmlPullParser. END_DOCUMENT switcheventType case XmlPullParser. START_TAG : ifxpp.getName.equalsIgnoreCase"item" start = true; else ifstart ifxpp.getName.equalsIgnoreCase"title" title = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"link" link = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"desc" description = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"pubDate" pubDate = xpp.nextText; break; .... 3 Parse the XML input stream with XMLPullParser .... case XmlPullParser. END_TAG : ifxpp.getName.equalsIgnoreCase"item" News newsItem = new Newstitle, link, description, pubDate; news.addnewsItem; start = false; break; eventType = xpp.next; 4 Parsing JSON options Via JSONObject & JSONArray Standard Android API In package org.json Via JsonReader Standard Android API since API Level 11 In package android.util.JsonReader Via Google GSON library http://code.google.com/p/google-gson/ In package com.google.gson 5 Parse the JSON input stream using JSONObject // parse a JSONObject JSONObject jObject = new JSONObjectresult; String title = json_data.getString"title" String link = json_data.getString"link" String description = json_data.getString"description" String pubDate = json_data.getString"pubDate" // Construct an Item object to store the data Item item = new Itemtitle, link, description, pubDate; 6 Parse the JSON input stream using JSONArray // parse a JSONArray JSONArray jArray = new JSONArrayresult; for int i = 0; i jArray.length; i++ // Each element of an JSONArray is a JSONObject JSONObject json_data = jArray.getJSONObjecti; String title = json_data.getString"title" String link = json_data.getString"link" String description = json_data.getString"description" String pubDate = json_data.getString"pubDate" // Construct an Item object to store the data Item item = new Itemtitle, link, description, pubDate; items.additem; 7 Parse a JSON Array using JsonReader InputStreamReader isr = new InputStreamReaderis; JsonReader jr = new JsonReaderisr; jr.beginArray; // start JSON Array handling whilejr.hasNext jr.beginObject; // parse each JSON Object in the JSON Array jr.endObject; jr.endArray; // end JSON Array handling jr.close; 8 Parse a JSON Object using JsonReader jr.beginObject; // start JSON Object handling whilejr.hasNext String name = jr.nextName; ifname.equals"level" String level = jr.nextString; else ifname.equals"number" String number = jr.nextString; else ifname.equals"nickname" String nickname = jr.nextString; else jr.skipValue; jr.endObject; // end JSON Object handling
Parsing JSON & XML 2 Parse the XML input stream with XMLPullParser XmlPullParser xpp = Xml. newPullParser ; xpp.setInputnew InputStreamReaderis; boolean start = false; int eventType = xpp.getEventType; whileeventType!= XmlPullParser. END_DOCUMENT switcheventType case XmlPullParser. START_TAG : ifxpp.getName.equalsIgnoreCase"item" start = true; else ifstart ifxpp.getName.equalsIgnoreCase"title" title = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"link" link = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"desc" description = xpp.nextText; else ifxpp.getName.equalsIgnoreCase"pubDate" pubDate = xpp.nextText; break; .... 3 Parse the XML input stream with XMLPullParser .... case XmlPullParser. END_TAG : ifxpp.getName.equalsIgnoreCase"item" News newsItem = new Newstitle, link, description, pubDate; news.addnewsItem; start = false; break; eventType = xpp.next; 4 Parsing JSON options Via JSONObject & JSONArray Standard Android API In package org.json Via JsonReader Standard Android API since API Level 11 In package android.util.JsonReader Via Google GSON library http://code.google.com/p/google-gson/ In package com.google.gson 5 Parse the JSON input stream using JSONObject // parse a JSONObject JSONObject jObject = new JSONObjectresult; String title = json_data.getString"title" String link = json_data.getString"link" String description = json_data.getString"description" String pubDate = json_data.getString"pubDate" // Construct an Item object to store the data Item item = new Itemtitle, link, description, pubDate; 6 Parse the JSON input stream using JSONArray // parse a JSONArray JSONArray jArray = new JSONArrayresult; for int i = 0; i jArray.length; i++ // Each element of an JSONArray is a JSONObject JSONObject json_data = jArray.getJSONObjecti; String title = json_data.getString"title" String link = json_data.getString"link" String description = json_data.getString"description" String pubDate = json_data.getString"pubDate" // Construct an Item object to store the data Item item = new Itemtitle, link, description, pubDate; items.additem; 7 Parse a JSON Array using JsonReader InputStreamReader isr = new InputStreamReaderis; JsonReader jr = new JsonReaderisr; jr.beginArray; // start JSON Array handling whilejr.hasNext jr.beginObject; // parse each JSON Object in the JSON Array jr.endObject; jr.endArray; // end JSON Array handling jr.close; 8 Parse a JSON Object using JsonReader jr.beginObject; // start JSON Object handling whilejr.hasNext String name = jr.nextName; ifname.equals"level" String level = jr.nextString; else ifname.equals"number" String number = jr.nextString; else ifname.equals"nickname" String nickname = jr.nextString; else jr.skipValue; jr.endObject; // end JSON Object handling