JSON OVERVIEW AND PARSING What is JSON Java

  • Slides: 17
Download presentation
JSON OVERVIEW AND PARSING

JSON OVERVIEW AND PARSING

What is JSON �Java. Script Object Notation �Used to format data �Commonly used in

What is JSON �Java. Script Object Notation �Used to format data �Commonly used in Web as a vehicle to describe data being sent between systems

JSON example � “JSON” stands for “Java. Script Object Notation” �Despite the name, JSON

JSON example � “JSON” stands for “Java. Script Object Notation” �Despite the name, JSON is a (mostly) language-independent way of specifying objects as name-value pairs � Example (http: //secretgeek. net/json_3 mins. asp): �{"skillz": { "web": [ { "name": "html", "years": 5 }, { "name": "css", "years": 3 }] "database": [ { "name": "sql", "years": 7 }] }}

JSON syntax �An object is an unordered set of name/value pairs �The pairs are

JSON syntax �An object is an unordered set of name/value pairs �The pairs are enclosed within braces, { } �There is a colon between the name and the value �Pairs are separated by commas �Example: { "name": "html", "years": 5 } �An array is an ordered collection of values �The values are enclosed within brackets, [ ] �Values are separated by commas �Example: [ "html", ”xml", "css" ]

JSON syntax �A value can be: A string, a number, true, false, null, an

JSON syntax �A value can be: A string, a number, true, false, null, an object, or an array �Values can be nested �Strings are enclosed in double quotes, and can contain the usual assortment of escaped characters �Numbers have the usual C/C++/Java syntax, including exponential (E) notation �All numbers are decimal--no octal or hexadecimal �Whitespace can be used between any pair of tokens

How to turn JSON into Java. Script object –eval(*) �The Java. Script eval(string) method

How to turn JSON into Java. Script object –eval(*) �The Java. Script eval(string) method compiles and executes the given string �The string can be an expression, a statement, or a sequence of statements �Expressions can include variables and object properties �eval returns the value of the last expression evaluated �When applied to JSON, eval returns the described object

JSON and—methods? �In addition to instance variables, objects typically have methods �There is nothing

JSON and—methods? �In addition to instance variables, objects typically have methods �There is nothing in the JSON specification about methods �However, a method can be represented as a string, and (when received by the client) evaluated with eval �Obviously, this breaks language-independence �Also, Java. Script is rarely used on the server side

Comparison of JSON and XML � Similarities: �Both are human readable �Both have very

Comparison of JSON and XML � Similarities: �Both are human readable �Both have very simple syntax �Both are hierarchical �Both are language independent �Both can be used by Ajax �Both supported in APIs of many programming languages � Differences: �Syntax is different �JSON is less verbose �JSON can be parsed by Java. Script’s eval method �JSON includes arrays �Names in JSON must not be Java. Script reserved words �XML can be validated

JSON in AJAX �JSON can be used in AJAX as follows: �Include it in

JSON in AJAX �JSON can be used in AJAX as follows: �Include it in HTML directly �<html>. . . <script> var data = JSONdata; </script>. . . </html> �JSON is used with XMLHttp. Request and can be converted into a Java. Script structure �response. Data = eval('(' + response. Text + ')'); We have not yet spoken about AJAX --- revisit this again after you have learned about AJAX

Why is JSON better suited for AJAX? � JSON is widely used in AJAX.

Why is JSON better suited for AJAX? � JSON is widely used in AJAX. The X in AJAX stands for XML. � E. g. �{ � "fullname": "Swati Kumar", � "org": "Columbia", �} � <? xml version='1. 0‘ encoding='UTF-8'? > � <element> � <fullname>Swati Kumar</fullname> � <org>Columbia</org> � </element>

�JSON response at client side is: �var name = eval('(' + req. response. Text

�JSON response at client side is: �var name = eval('(' + req. response. Text + ')'). fullname. value; �To access a composite element �eval('(' + req. response. Text + ')'). xyz. abc. value; �Thus, any level deep elements can be easily accessed.

XML and Java. Script �XML response at client side is: �var root = req.

XML and Java. Script �XML response at client side is: �var root = req. response. XML; �var name = root. get. Elements. By. Tag. Name(‘fullname’); �To access a composite element �root. get. Elements. By. Tag. Name(‘xyz’)[0]. first. Child �To access deeper levels we need more overhead. �Reduced extensibility in XML

YAML – another option? �YAML can be taken as an acronym for either �Yet

YAML – another option? �YAML can be taken as an acronym for either �Yet Another Markup Language �YAML Ain’t Markup Language �Like JSON, the purpose of YAML is to represent typical data types in human-readable notation �YAML is (almost) a superset of JSON, with many more capabilities (lists, casting, etc. ) �Except: YAML doesn’t handle escaped Unicode characters �Consequently, JSON can be parsed by YAML parsers � When JSON isn’t enough, consider YAML

JSON and Java HOW TO PARSE JSON IN JAVA

JSON and Java HOW TO PARSE JSON IN JAVA

Mapping between JSON and Java entities JSON Java string java. lang. String number java.

Mapping between JSON and Java entities JSON Java string java. lang. String number java. lang. Number true|false java. lang. Boolean null array java. util. List object java. util. Map JSON. simple maps entities from the left side to the right side while decoding or parsing, and maps entities from the right to the left while encoding. On decoding, the default concrete class of java. util. List isorg. json. simple. JSONArray and the default concrete class of java. util. Map isorg. json. simple. JSONObject.

JSON reading in Java example �http: //www. tutorialspoint. com/json_java_exa mple. htm

JSON reading in Java example �http: //www. tutorialspoint. com/json_java_exa mple. htm

There is an interface called JSONParser in javax. json. stream you can implement �javax.

There is an interface called JSONParser in javax. json. stream you can implement �javax. json. stream �Interface Json. Parser