1
Parsing JSON, Using Libraries, Java Collections, Generics
Slides adapted from Craig Zilles
Parsing JSON, Using Libraries, Java Collections, Generics Slides - - PowerPoint PPT Presentation
Parsing JSON, Using Libraries, Java Collections, Generics Slides adapted from Craig Zilles 1 CamelCaser Difficulty How difficult was the first assignment? A. Easy B. Moderate C. Challenging D. Unreasonable 2 CamelCaser Time How long did
1
Slides adapted from Craig Zilles
2
3
4
¢ JavaScript Object Notation ¢ A lightweight data-interchange format
¢ It is easy for humans to read and write. ¢ It is easy for machines to parse and generate.
5
6
¢ API = Application Programming Interface ¢ Get an API key ¢ Grab some JSON:
¢ JSON formatter/pretty printer
7
¢ Use the GSON library from Google
¢ Build classes with fields for the desired elements of the JSON
¢ Instantiate a Gson object
¢ Use the fromJSON method to parse the JSON
¢ Extended example using NewsAPI
8
¢ E.g., only select those articles with specific authors ¢ What should be the return type of such a function?
9
public NewsArticle[] removeNullAuthorArticles(NewsArticle[] input) { // output array can’t be bigger than input array NewsArticle [] output = new NewsArticle[input.length]; int outputIndex = 0; for (int i = 0; i < input.length; i++) { if (input[i].getAuthor() != null) {
} } return output; }
10
¢ collection: an object that stores data; a.k.a. "data structure"
§ ArrayList, HashMap, TreeSet
import java.util.*;
11
12
¢ list: a collection storing an ordered sequence of elements
13
add(value) appends value at end of list add(index, value) inserts given value just before the given index, shifting subsequent values to the right clear() removes all elements of the list indexOf(value) returns first index where given value is found in list (-1 if not found) get(index) returns the value at given index remove(index) removes/returns value at given index, shifting subsequent values to the left set(index, value) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as "[3, 42, -7, 15]"
14
¢ When constructing an ArrayList, you must specify the
15
¢ Can’t do ArrayList<int> ¢ Java provides “boxed primitives”: E.g., Integer
¢ Can do:
Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean
16
¢ Read chapter 4 of your book “Aesthetics” ¢ Read section 4 (Formatting) of the Google Java Style Guide ¢ Take Policy Quiz ¢ Assignment for next week’s code review: