Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials Json "JSONArray" example in Java

Related Tutorials


 
 

Share on Google+Share on Google+

"JSONArray" example in Java

Advertisement
In this part of JSON tutorial you will study how to use JSONArray in Java. JSONArray is a sequential and ordered way of collection values.

"JSONArray" example in Java

     

In this part of JSON tutorial you will study how to use JSONArray in Java. JSONArray is a sequential and ordered way of collection values.It may consists of Boolean, JSONArray, JSONObject, Number and String or the JSONObject.NULL objects.

To have functionality of JSON in your java program you must have JSON-lib. JSON-lib also requires following "JAR" files:

  1. commons-lang.jar 
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. ezmorph.jar
  6. json-lib-2.2.2-jdk15.jar

JSON-lib is a java library for that transforms beans, collections, maps, java arrays and XML to JSON and then for retransforming them back to beans, collections, maps and others.

In this example we are going to use JSONArray for creating an object of JSONArray and then we will print this array object . For using JSONArray class we have to import package "net.sf.json". To add elements in this object we have used add() method. Here is the full example code of JSONJavaArray.java  as follows:

FirstJSONJava.java

import net.sf.json.JSONArray;

public class JSONJavaArray
{
  public static void main(String args[]){
   JSONArray arrayObj=new JSONArray();
 arrayObj.add("name :");
 arrayObj.add("Amit Kumar");
 arrayObj.add("Max.Marks :");
 arrayObj.add(new Integer(100));
 arrayObj.add("Min.Marks");
 arrayObj.add(new Double(40));
 arrayObj.add("Scored");
 arrayObj.add(new Double(66.67));
 System.out.println(arrayObj);
  }
}  

To run this example you have to follow these few steps as follows:

Output:


Download Source Code

 

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 27, 2008

Related Tutorials

Discuss: "JSONArray" example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Shivaraj RH
September 6, 2011
Thanks

I like this Rose India all articles. Thank u so much your detailed info.
Kinjan
October 11, 2011
List to Jason Array

Thanks to rose india....i got solution for convering List to json array........
krishna
December 2, 2011
error

i m getting below error java.lang.ClassNotFoundException: net.sf.json.JSONArray org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533) org.eclipse.trans.JsonTransServlet.doGet(JsonTransServlet.java:34) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
naveed khan
September 6, 2012
Comment

Thank you very helpfull example..
Ravi
October 26, 2012
json array

the output is wrong. comma separates the name ,value pair. the correct list should look like this: [ "name" : "Amit Kumar" , "Max.Marks" : " 100" ] the example generates this [ "name :", "Amit Kumar" , "Max.Marks:" :," 100" ]
Jefferson Ferregato
October 14, 2013
Obrigado! Thanks!

Ajudou muito! Thank u so much!
DMCA.com