4:44 pm - Sunday July 23, 2017

JSON Array With JAVA

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

JsonArray:

JSONArray holds values.
It is an ordered value.
It supports any data types (Boolean,Number,JSONArray,JSONObject,String).

Syntax:

JSONArray array=new JSONArray();
array.put("value");

Another Format:

Its external form is a string wrapped in square brackets with commas separating the values.

String json =" [ value, value ] ";
JSONArray array=new JSONArray(json);

In JSONArray values are processed usig array index position.
Position starts from one.

Methods:

Put-put method is used to store values in JSONArray.
opt-This method returns the value of the given position. If the given position does not exist it returns default value, null value.
get – This method returns value of given position. If the given position does not exist it throws an exception.
length – This method returns the length of the jsonObject. Counting starts from one.
isNull – This method returns whether the given position has null value.
has – This method used to find whether the array has the given position.

Project Structure:

 

 

 

 

 

 

 

 

 

JARFiles: 

 

  

JArray.java:

package com.candidjava;

import org.json.JSONArray;
import org.json.JSONException;

public class JArray {
	public static void main(String args[]) throws JSONException {
		JSONArray arrayObj = new JSONArray();
		arrayObj.put("name :");
		arrayObj.put("Deepa");
		arrayObj.put("Reg No");
		arrayObj.put(123);
		arrayObj.put("Mark");
		arrayObj.put(new Double(90));
		arrayObj.put("City");
		arrayObj.put("Chennai");

		System.out.println(arrayObj);

		int length = arrayObj.length();
		String opt = arrayObj.optString(2);
		boolean data = arrayObj.isNull(3);
		String getMark = arrayObj.getString(5);
		Object ob = arrayObj.get(7);

		System.out.println("Length: " + length);
		System.out.println("2nd Position: " + opt);
		System.out.println("isNull: " + data);
		System.out.println("Mark:" + getMark);
		System.out.println("City: " + ob);

	}
}

OUTPUT:

[“name :”,”Deepa”,”Reg No”,123,”Mark”,90,”City”,”Chennai”]
Length: 8
2nd Position: Reg No
isNull: false
Mark:90.0
City: Chennai

DOWNLOAD+SOURCECODE

Filed in: Z-A JSON codes

Comments are closed.