json in java
DESCRIPTION
JSON is JavaScript Object Notation [ it's the damn default definition ], and was invented by Douglas Crockford. Actually JSON is lightweight alternative to XML,language and platform independent script.TRANSCRIPT
![Page 1: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/1.jpg)
1
Apex T. G. India Pvt. Ltd
Json with Java
![Page 2: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/2.jpg)
1
Json JSON is JavaScript Object Notation [ it's the damn default
definition ], and was invented by Douglas Crockford. Actually
JSON is lightweight alternative
to XML,language and platform independent script.
Even JSON parsers & libraries are exists for
many programming languages.
![Page 3: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/3.jpg)
1
Json
Converting JSON text to something in JavaScript will takes
one line of code by using eval ( ) function , to create an object
from JSON text, just pass the string to eval ( ) function after
surrounding the text with parentheses that’s it.
![Page 4: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/4.jpg)
1
JSON Object From JSON textJSON Object:
var names = "{"nameslist": ["java","php",“dotnet"]}";
The variable names will hold the JSON textWhole JSON text must be surrounded by quotes
![Page 5: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/5.jpg)
1
Variable (names) Into JS object Actually we can convert JSON text into JavaScript object
with eval function. eval() . eval function. eval() is the JavaScript function which
converts JSON text into an JavaScript object.
For Example:var jsObject = eval("(" + names + ")");
![Page 6: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/6.jpg)
1
Access JSON Object var arr_name = jsObject["nameslist"];
arr_name[0] will prints - java arr_name[1] will prints - php arr_name[2] will prints – dotnet Note: Index always start from zero
Instead of using eval() function It is safer to use a JSON parser to convert a JSON text to a JS object.
A JSON parser will recognize only JSON text and will not compile scripts.
![Page 7: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/7.jpg)
1
JSON text to JS Object JSON arrays are written inside square brackets Array can contain any number of objectsFor Example:
{"names": ['+'{"name" : “Shashi", "address" : “Patna", "age" : 26},'+'{"name" : “Kamlesh", "address" : “Delhi", "age" : 28}'+']}
Here names is an array containing 2 objects. By using JavaScript syntax we can access the values with the help of “.” operator,
![Page 8: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/8.jpg)
1
JSON text to JS Object Example:
<script language="javascript" >
var JsonObjecttext = {"names": ['+'{"name" : “Shashi", "address" : “Patna", "age" : 26},'+'{"name" : “Kamlesh", "address" : “Delhi", "age" : 28}'+']}
var JsonObjectName = eval ("(" + JsonObjecttext + ")"); document.write("<font color='#148ee3' size='2' face='verdana'>Name - <font color='#000'>" +JsonObjectName.names[0].name+"</font></font> || ");document.write("<font color='#148ee3' size='2' face='verdana'>Age. - <font color='#000'>" +JsonObjectName.names[0].age+"</font></font> || ");document.write("<font color='#148ee3' size='2' face='verdana'>name - <font color='#000'>" +JsonObjectName.names[1].name+"</font></font> || ");document.write("<font color='#148ee3' size='2' face='verdana'>name - <font color='#000'>" +JsonObjectName.names[1].age+"</font></font>"); </script>
![Page 9: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/9.jpg)
1
Requirement to use Json in javaJars required:
json-lib-2.2.2-jdk15.jarezmorph.jarcommons-lang.jarcommons-logging.jarcommons-beanutils.jarcommons-collections.jar
![Page 10: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/10.jpg)
1
Json in javaimport net.sf.json.JSONObject;public class JsonEx { public static void main(String args[]) { JSONObject jo=new JSONObject(); jo.put(“company",“apex tgi"); jo.put(“address",“E-20,Sec-63,Noida"); jo.put(“state",”UP”); System.out.println(jo); } }
![Page 11: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/11.jpg)
1
Contact us
![Page 12: Json in java](https://reader036.vdocuments.mx/reader036/viewer/2022081506/558fc9be1a28ab970b8b46e8/html5/thumbnails/12.jpg)
1Thanks
facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi
Stay Connected with us for more chapters on JAVA