Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Monday, November 5, 2012

How to iterate Java object in JavaScript using JSON?


You may require passing a java object to javascript and iterating through it. Java object could be literally anything whether it List, Map, List of Map, Map of List etc.
For example: ArrayList<String>, Hashtable<String,String>, ArrayList<Hashtable<String,String>>, Hashtable<String,ArrayList<String>> etc.

This can be easily achieved using JSON. To run below JSP you need to include json-simple-1.1.1.jar in classpath. Download the JAR file from http://code.google.com/p/json-simple/

About the example: In below example I have created a java object i.e. HashMap<String, ArrayList<String>>. Add some values to the object. Convert the object into JSON string using method JSONValue.toJSONString(). In JavaScript pass the JSON string to JSON.parse() and get the corresponding JavaScript object. Now you are ready to iterate it using below syntax. Please see the self explanatory example below.

Friday, November 2, 2012

Convert an object to json string and vice versa in Java and Javascript

Today we will see how to convert an object to json string and vice versa. An object could be JSONObject or any Java Object for example Map of list, List of map etc. Also please see my earlier post on Java Examples for JSON encoding, click here.

Note - To run below examples you need to include json-simple-1.1.1.jar in classpath. Download the JAR file from http://code.google.com/p/json-simple/

Example 1 : Convert JSONObject to JSON String and vice versa

Output :

{"Name":"Ramesh","Nickname":null,"Salary":15000.0,"isPermanent":true,"EmployeeId":121}
121
Ramesh
15000.0
true
null

Thursday, November 1, 2012

JSTL: How to access fmt:message from Javascript


You might have seen How to use fmt:message and ResourceBundle from JSP and Java in my earlier post, click here.

Now we will see how to use fmt:message from Javascript. Its very simple to use JSTL's fmt tag in JavaScript to localize alert messages.

I found two ways in which you can access values from a property file in javascript. Please see the sample codes below.

Method 1.

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<script type="text/javascript">
<!--
var msg = {
prop1: "<fmt:message key="prop1" />",
prop2: "<fmt:message key="prop2" />"
};

function test() {  
 alert(msg.prop1);
alert(msg.prop2);
}

//-->
</script>

Method 2.

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<script type="text/javascript">
<!--
<fmt:message key="prop1" var="prop1"/>    
var prop1 = "${prop1}";

function test() {  
 alert(prop1);
}

//-->
</script>

Friday, April 20, 2012

Java Examples for JSON encoding

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is syntax for storing and exchanging text information same like XML. JSON is smaller than XML, and faster and easier to parse.

To know more about JSON, visit http://www.json.org/
Java API http://www.json.org/java/

Json can be used to commincate between Java & Javascript and vice versa. Lets see a simple self explanatory example.

About the example :
A simple JSP page which contains Java instance of JSONObject. This object is converted to String and passed to eval() function in javascript to convert into JSONObject object in Javascript.

To run below example you need to include json-simple-1.1.1.jar in classpath. Download the JAR file from http://code.google.com/p/json-simple/

<%@ page import="org.json.simple.JSONObject" %>
<%
 JSONObject jObject = new JSONObject();

 jObject.put("EmployeeId",new Integer(121));
 jObject.put("Name","Ramesh");  
 jObject.put("Salary",new Double(15000.00));
 jObject.put("isPermanent",new Boolean(true));
 jObject.put("Nickname",null);

 String jsonText = jObject.toJSONString();

 System.out.println("jsonText :: "+jsonText);
%>
<html>
<head>
<script type="text/javascript">
 function loadJsonObject()
 {
  var JSONObject = eval('(<%= jsonText %>)');

  document.getElementById("jId").innerHTML=JSONObject.EmployeeId;
  document.getElementById("jName").innerHTML=JSONObject.Name;
  document.getElementById("jSalary").innerHTML=JSONObject.Salary;
  document.getElementById("jParmanent").innerHTML=JSONObject.isPermanent;
  document.getElementById("jNickname").innerHTML=JSONObject.Nickname;

 }
</script>
</head>
<body onload="loadJsonObject()">
<h2>Simple JSON Example</h2>
<p>
<table border="1" width="100%">
<tr>
 <td>Employee Id</td>
 <td><span id="jId"></span</td>
</tr>
<tr>
 <td>Name</td>
 <td><span id="jName"></span></td>
</tr>
<tr>
 <td>Salary</td>
 <td><span id="jSalary"></span></td>
</tr>
<tr>
 <td>Is Permananet</td>
 <td><span id="jParmanent"></span></td>
</tr>
<tr>
 <td>Nickname</td>
 <td><span id="jNickname"></span></td>
</tr>
</table>
</body>
</html>

Output :






Note : If you get mentioned errors.
  • Only a type can be imported. org.json.simple.JSONObject resolves to a package
  • JSONObject cannot be resolved to a type
Crosscheck if json-simple-1.1.1.jar is set in classpath. Simply add the JAR file to ${Tomcat}/lib/ folder. Please download json-simple-1.1.1.jar file from http://code.google.com/p/json-simple/ and set it to classpath.