json in java
Post on 28-Jun-2015
161 Views
Preview:
DESCRIPTION
TRANSCRIPT
1
Apex T. G. India Pvt. Ltd
Json with Java
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.
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.
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
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 + ")");
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.
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,
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>
1
Requirement to use Json in javaJars required:
json-lib-2.2.2-jdk15.jarezmorph.jarcommons-lang.jarcommons-logging.jarcommons-beanutils.jarcommons-collections.jar
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); } }
1
Contact us
1Thanks
facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi
Stay Connected with us for more chapters on JAVA
top related