Consuming Java Script Object Notation (JSON) feeds

25
Consuming Java Script Object Notation (JSON) feeds

description

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. - PowerPoint PPT Presentation

Transcript of Consuming Java Script Object Notation (JSON) feeds

Page 1: Consuming  Java Script Object Notation (JSON) feeds

Consuming Java Script Object

Notation (JSON) feeds

Page 2: Consuming  Java Script Object Notation (JSON) feeds

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" }]}

Page 3: Consuming  Java Script Object Notation (JSON) feeds

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

Page 4: Consuming  Java Script Object Notation (JSON) feeds

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" }]}

Page 5: Consuming  Java Script Object Notation (JSON) feeds

String Class

Page 6: Consuming  Java Script Object Notation (JSON) feeds

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.

Page 7: Consuming  Java Script Object Notation (JSON) feeds

Example

Page 8: Consuming  Java Script Object Notation (JSON) feeds

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.

Page 9: Consuming  Java Script Object Notation (JSON) feeds

Example

Page 10: Consuming  Java Script Object Notation (JSON) feeds

Comparing Strings

Strings are compared using one of the methods below boolean equals

boolean equalsIgnoreCase

int compareTo

boolean regionMatches

Page 11: Consuming  Java Script Object Notation (JSON) feeds

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

Page 12: Consuming  Java Script Object Notation (JSON) feeds

String startsWith and endsWith

Page 13: Consuming  Java Script Object Notation (JSON) feeds

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 1st int is an integer representation of the character the 2nd represents an index from which searching begins

Page 14: Consuming  Java Script Object Notation (JSON) feeds

Extracting substring from Strings

Page 15: Consuming  Java Script Object Notation (JSON) feeds

Concatenating strings

Page 16: Consuming  Java Script Object Notation (JSON) feeds

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.

Page 17: Consuming  Java Script Object Notation (JSON) feeds

StringBuilder

Page 18: Consuming  Java Script Object Notation (JSON) feeds

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.

Page 19: Consuming  Java Script Object Notation (JSON) feeds

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.

Page 20: Consuming  Java Script Object Notation (JSON) feeds

Example

Page 21: Consuming  Java Script Object Notation (JSON) feeds

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

Page 22: Consuming  Java Script Object Notation (JSON) feeds

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

Page 23: Consuming  Java Script Object Notation (JSON) feeds

Character

Page 24: Consuming  Java Script Object Notation (JSON) feeds

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

Page 25: Consuming  Java Script Object Notation (JSON) feeds

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