What is Json in Java and How to access them

12
8/10/2019 What is Json in Java and How to access them http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 1/12 Apex T. G. India Pvt. Ltd Json with Java

Transcript of What is Json in Java and How to access them

Page 1: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 1/12

Apex T. G. India Pvt. Ltd

Json with Java

Page 2: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 2/12

Page 3: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 3/12

1

Json "onvertin! JSON te+t to somethin! in JavaScript will ta#es

one line of code by usin! eval - function , to create an

object from JSON te+t, just pass the strin! to eval - function

after surroundin! the te+t with parentheses that.s it$

Page 4: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 4/12

1

JSON Object /rom JSON te+t JSON Object:

var names 0 121nameslist13 [1java1,1php1,4dotnet1]516

7he variable names will hold the JSON te+t 

8hole JSON te+t must be surrounded by 9uotes

Page 5: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 5/12

1

:ariable names- ;nto JS object  %ctually we can convert JSON te+t into JavaScript object

with eval function$ eval- $

eval function$ eval- is the JavaScript function which

converts JSON te+t into an JavaScript object$

/or )+ample3

var jsObject = eval("(" + names + ")");

Page 6: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 6/12

1

%ccess JSON Object  var arr<name 0 jsObject[1nameslist1]6

arr<name[=] will prints > java

arr<name[?] will prints > php

arr<name[@] will prints A dotnet  Note3 ;nde+ always start from Bero

;nstead of usin! eval- function ;t is safer to use

a JSON parser to convert a JSON te+t to a JS object$

 % JSON parser will reco!niBe only JSON te+t and will

not compile scripts$

Page 7: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 7/12

1

JSON te+t to JS Object  JSON arrays are written inside s9uare brac#ets

%rray can contain any number of objects

/or )+ample3

21names13 ['C'21name1 3 4Shashi1, 1address1 3 4Datna1, 1a!e1 3 @E5,'C'21name1 3

4Famlesh1, 1address1 3 4elhi1, 1a!e1 3 @G5'C']5

Here names is an array containin! @ objects$ Iy

usin! JavaScript synta+ we can access the values withthe help of 4$ operator,

Page 8: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 8/12

1

JSON te+t to JS Object  )+ample3

<script language="javascript" >

 

var JsnObjectte!t = 21names13 ['C'21name1 3 4Shashi1, 1address1 3 4Datna1, 1a!e1 3

@E5,'C'21name1 3 4Famlesh1, 1address1 3 4elhi1, 1a!e1 3 @G5'C']5

 

var JsnObjectName = eval ("(" + JsnObjectte!t + ")");

 

cument#$rite("<%nt clr=&'*ee& si,e=&-& %ace=&verana&>Name . <%nt

clr=&'///&>" +JsnObjectName#names0/1#name+"<2%nt><2%nt> 33 ");

cument#$rite("<%nt clr=&'*ee& si,e=&-& %ace=&verana&>4ge# . <%nt

clr=&'///&>" +JsnObjectName#names0/1#age+"<2%nt><2%nt> 33 ");

cument#$rite("<%nt clr=&'*ee& si,e=&-& %ace=&verana&>name . <%nt

clr=&'///&>" +JsnObjectName#names01#name+"<2%nt><2%nt> 33 ");

cument#$rite("<%nt clr=&'*ee& si,e=&-& %ace=&verana&>name . <%nt

clr=&'///&>" +JsnObjectName#names01#age+"<2%nt><2%nt>");

<2script>

Page 9: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 9/12

1

Ke9uirement to use Json in javaJars re9uired3

?- json>lib>@$@$@>jd#?L$jar

@- eBmorph$jar

M- commons>lan!$jar

- commons>lo!!in!$jar

L- commons>beanutils$jar

E- commons>collections$jar

Page 10: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 10/12

1

Json in javaimport net$sf$json$JSONObject6

public class Json)+ 2

 

public static vi mainString ar!s[]-  2

  JSONObject jo0new JSONObject-6

  jo$put4company1,4ape+ t!i1-6

  jo$put4address1,4)>@=,Sec>EM,Noida1-6

  jo$put4state1,D-6  System$out$printlnjo-6

  5

 

5

Page 11: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 11/12

1

"ontact us

Page 12: What is Json in Java and How to access them

8/10/2019 What is Json in Java and How to access them

http://slidepdf.com/reader/full/what-is-json-in-java-and-how-to-access-them 12/12

Thanks

facebook.com/apex.tgi

twitter.com/ApextgiNoida

 pinterest.com/apextgi

Stay Connected with us for more chapters on JAVA