Vous êtes sur la page 1sur 12

Json with Java

Apex T. G. India Pvt. Ltd

Json

JSON is JavaScriptObjectNotation [ it's the damn default


definition], and was invented byDouglas Crockford.
Actually JSON islightweightalternative
toXML,languageandplatformindependent script.
EvenJSONparsers & libraries are exists for
manyprogramminglanguages.

Json

ConvertingJSONtext to something inJavaScriptwill takes


one line of code by usingeval ( ) function, to create an
object from JSON text, just pass the string to eval ( ) function
after surrounding the text withparenthesesthats it.

JSON Object From JSON text


JSON Object:
var names = "{"nameslist": ["java","php",dotnet"]}";

The variablenameswill hold the JSON text


WholeJSONtext must be surrounded by quotes

Variable (names) Into JS object

Actually we can convertJSONtext into JavaScriptobject


with evalfunction.eval() .
evalfunction.eval() is the JavaScriptfunction which
convertsJSONtext into an JavaScript object.

For Example:
var jsObject = eval("(" + names + ")");

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 usingeval() function It is safer to use


aJSONparser to convert a JSON text to a JSobject.

A JSON parser willrecognizeonly JSON text and will


notcompile scripts.

JSON text to JS Object


JSONarrays are written insidesquarebrackets
Arraycan contain any number ofobjects
For Example:

{"names": ['+'{"name" : Shashi","address": Patna","age": 26},'+'{"name" :


Kamlesh","address": Delhi", "age": 28}'+']}

Here names is anarraycontaining2objects. By


usingJavaScriptsyntax we can access the values with
thehelpof .operator,

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>

Requirement to use Json in java


Jars required:
1)
2)
3)
4)
5)
6)

json-lib-2.2.2-jdk15.jar
ezmorph.jar
commons-lang.jar
commons-logging.jar
commons-beanutils.jar
commons-collections.jar

Json in java
import 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);
}

Contact us

Thanks

Stay Connected with us for more chapters on JAVA

facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi