ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Struts2 and JSON notes

Updated on January 13, 2014

JSON (JavaScript Object Notation)

  • is an independent data exchange format
  • is syntax for storing and exchanging text information, much like XML.
  • is a subset of the JavaScript Specification (ECME-Script) and it is therefore directly supported in JavaScript
  • is smaller than XML, and faster and easier to parse
  • language independent
  • is "self-describing" and easy to understand
  • limitation: Limited to text and numeric values. Binary values are not supported
  • data structure: key / value pair(s). [key is string, value is numerical value / a boolean value (true or false) or an object]
  • JSON object: is a set of key / value pairs which starts with "{" and ends with "}"
  • Lists of one or more values surrounded by [] and separated by ","

JSON Example

For a single JSON object, use a set of key / value pairs which starts with "{" and ends with "}".

{
	firstName:'firstName1',
	lastName:'lastName1',
	address: {
		street:'Street1',
		state: 'NY'
	}
}

If more JSON objects then lists are one or more values surrounded by [] and separated by ",".

[
	{
		firstName:'firstName1',
		lastName:'lastName1',
		address: {
			street:'Street1',
			state: 'NY'
		}
	}
	,
	{
		firstName:'firstName2',
		lastName:'lastName2',
		address: {
			street:'Street2',
			state: 'VA'
		}
	}
]
<html>
	<body>
		<script type="text/javascript">
			
			<!--Evaluate the object and assign to variables -->
			var user = {
				firstName:'firstName1',
				lastName:'lastName1',
				address: {
					street:'Street1',
					state: 'NY'
				}
			};
			
			<!--Use the object-->
			alert(user.firstName + ' lives in ' + user.address.state + ' state.');
			document.writeln(user.firstName + ' ' +user.lastName);
		</script>
	</body>
</html>

Steps to use JSON with Struts 2

Follow the following steps, to use JSON to your Java application:

  1. Download and add struts2-json-plugin.jar into your Java project library
  2. Define Action class which will be converted into JSON format
  3. Declare a package which extends the json-default & result type as json into your struts.xml file

Note: if there is no getter method, that data will not include in the JSON

JSON for simple String

In this example, we will create a String and we will convert an object into JSON format.

struts.xml configuration

	<package name="default" namespace="/" extends="json-default,struts-default">
		<action name="JSON" class="com.hubpages.plusminus.TestActionJSON" method="executeJSON">
			<result type="json" />
		</action>
	</package>

Java action class

package com.hubpages.plusminus;

import com.opensymphony.xwork2.ActionSupport;

public class TestActionJSON extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String SimpleString = "plus minus";

	public String getSimpleString() {
		return SimpleString;
	}

	public void setSimpleString(String simpleString) {
		SimpleString = simpleString;
	}

	public String execute() {
		return SUCCESS;
	}

}

Sample output

JSON for array

In this example, we will create a Array and we will convert an object into JSON format.

Java action class

package com.hubpages.plusminus;

import com.opensymphony.xwork2.ActionSupport;

public class TestActionJSON extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String[] stringArray = { "hubpages", "plusminus" };
	private int[] intArray = { 0, 1, 2, 3, 4, 5, 6 };

	public String[] getStringArray() {
		return stringArray;
	}

	public void setStringArray(String[] stringArray) {
		this.stringArray = stringArray;
	}

	public int[] getIntArray() {
		return intArray;
	}

	public void setIntArray(int[] intArray) {
		this.intArray = intArray;
	}

	public String executeJSON() {
		return SUCCESS;
	}

	public String jsonOutput() {
		return SUCCESS;
	}

}

Sample output

JSON for ArrayList

In this example, we will create a List and we will convert an object into JSON format.

Java code

package com.hubpages.plusminus;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class TestActionJSON extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private List<String> listMonthsOfYear = new ArrayList<String>();

	public List<String> getMonthsOfYear() {

		listMonthsOfYear.add("January");
		listMonthsOfYear.add("February");
		listMonthsOfYear.add("March");
		listMonthsOfYear.add("April");
		listMonthsOfYear.add("May");
		listMonthsOfYear.add("June");
		listMonthsOfYear.add("July");
		listMonthsOfYear.add("August");
		listMonthsOfYear.add("September");
		listMonthsOfYear.add("October");
		listMonthsOfYear.add("November");
		listMonthsOfYear.add("December");

		return listMonthsOfYear;
	}

	public void setMonthsOfYear(List<String> monthsOfYear) {
		this.listMonthsOfYear = monthsOfYear;
	}

	public String executeJSON() {

		return SUCCESS;
	}

	public String jsonOutput() {
		return SUCCESS;
	}

}

Sample output

JSON for HashMap

In this example, we will create a map and we will convert an object into JSON format.

Java code

package com.hubpages.plusminus;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import com.opensymphony.xwork2.ActionSupport;

public class TestActionJSON extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Map<String, String> mapMonthsOfYear = new HashMap<String, String>();

	public Map<String, String> getMapMonthsOfYear() {

		mapMonthsOfYear.put("key1", "January");
		mapMonthsOfYear.put("key2", "February");
		mapMonthsOfYear.put("key3", "March");
		mapMonthsOfYear.put("key4", "April");
		mapMonthsOfYear.put("key5", "May");
		mapMonthsOfYear.put("key6", "June");
		mapMonthsOfYear.put("key7", "July");
		mapMonthsOfYear.put("key8", "August");
		mapMonthsOfYear.put("key9", "September");
		mapMonthsOfYear.put("key10", "October");
		mapMonthsOfYear.put("key11", "November");
		mapMonthsOfYear.put("key12", "December");

		return mapMonthsOfYear;
	}

	public void setMapMonthsOfYear(Map<String, String> mapMonthsOfYear) {
		this.mapMonthsOfYear = mapMonthsOfYear;
	}

	public String executeJSON() {

		return SUCCESS;
	}

	public String jsonOutput() {
		return SUCCESS;
	}

}

Sample output

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article