250 likes | 337 Views
Consuming Java Script Object Notation (JSON) feeds. What is JSON? . JSON stands for J ava S cript O bject N otation is syntax for storing and exchanging text information Much like XML is smaller than XML, and faster and easier to parse.
E N D
What is JSON? • JSON • stands for JavaScript Object Notation • is syntax for storing and exchanging text information • Much like XML • is smaller than XML, and faster and easier to parse {"employees": [{ "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" }]}
Contrasting XML to JSON • Similarities: • both are • Plain-text • Self-describing (human readable) • Hierarchical (values nested within values) • Differences: • JSON • Uses no end tags • Is shorter • Quicker to read and write • Uses arrays
JSON syntax • JSON data • written as name/value pairs • Separated by commas (,) • JSON objects • Enclosed in curly brackets ({}) • JSON arrays • Delineated by square brackets ([]) "firstName" : "John" { "firstName":"John" , "lastName":"Doe" } {"employees": [{ "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" }]}
String constructors • No-argument constructor creates a • String that contains no characters • (i.e., the empty string, which can also be represented as "") and • has a length of 0. • Constructor that takes a • String object • copies the argument into the new String. • char array creates • a String containing a copy of the characters in the array. • char array and 2 integers creates • a String containing the specified portion of the array.
String length, charAt, and getChars methods • String method • length • determines the number of characters in a string. • charAt • returns the character at a specific position in the String. • getChars • copies the characters of a String into a character array. • The 1st argument is the starting index in the source String. • The 2nd argument is the index that is one past the last character in source String. • The 3rd argument is the character array into which the characters are to be copied. • The 4th argument is the starting index where the copied characters are placed in the target character array.
Comparing Strings • Strings are compared using one of the methods below • boolean equals • boolean equalsIgnoreCase • int compareTo • boolean regionMatches
Comparing Strings • Strings are compared using one of the methods below • boolean equals • returns true if the contents of the objects are equal and false otherwise • boolean equalsIgnoreCase • ignores the case when performing comparison • int compareTo • returns • 0 if contents are equal • A negative number if string invoking compareTo < argument • A positive number if string invoking compareTo > argument • boolean regionMatches • compares portions of two strings for equality
Locating characters and substrings in Strings • The following methods can be used: • int indexOf • 1st version locates the 1st occurrence of a char in a String returning • its index, or -1 otherwise • 2nd version takes two arguments, a char and an int parameters • the int represents the starting index at which search begins • int lastIndexOf • 1st version locates the last occurrence of a char in a String returning • its index, or -1 otherwise • 2nd version takes two int arguments • the 1stint is an integer representation of the character • the 2nd represents an index from which searching begins
Miscellaneous String methods • replace • returns a new String object in which • every occurrence of the first argument is replaced with the second. • toUpperCase • generates a new String with uppercase letters. • toLowerCase • returns a new String object with lowercase letters. • trim generates a new String object that • removes all whitespace characters that • appear at the beginning or end of the String on which trim operates. • Method toCharArray creates a new character array • containing a copy of the characters in the String.
StringBuilder • Class StringBuilder is used to • create and manipulate dynamic string information. • Every StringBuilder is capable of • storing a number of characters specified by its capacity. • If the capacity of a StringBuilder is exceeded, • the capacity expands to accommodate • the additional characters.
StringBuilder constructors • No-argument constructor creates a • StringBuilder with no characters in it and • an initial capacity of 16 characters. • Constructor that takes an integer argument • creates a StringBuilder with no characters in it and • the initial capacity specified by the integer argument. • Constructor that takes a String argument creates • a StringBuilder containing the characters in the argument. • The initial capacity is the number of characters in the argument + 16.
More StringBuilder methods • charAt • returns the character at the specified index • getChars • Copies characters from a StringBuilder into a char array • setCharAt • Takes an int and a char arguments • Setting the character at specified position to the character argument • reverse • Reverses the content of a StringBuilder
append, insert, delete, deleteCharAt methods • append • appends values of various types to end of StringBuilder • insert • inserts values at any position in a StringBuilder • delete • takes two indexes defining the portion to be deleted • deleteCharAt • takes a single index argument, index of character to delete
Methods of Character class • isDigit • determines whether a character is a digit • isLetter • determines whether a character is a letter • isLetterOrDigit • determines whether a character is a letter or digit • isLowerCase/isUpperCase • determines whether a character is lowercase/uppercase
Methods of Character class (cont’d) • toUpperCase • converts a character into its uppercase equivalent • toLowerCase • converts a character into its lowercase equivalent • charValue • returns the character stored in the object • equals • determines whether 2 characters have the same content