CS 4385 - Topics in Software Engineering

星期二, 9月 13, 2005

JSON

Introduction of JSON

JSON is a string like structure to exchange information between client and server.
Except data (string, boolean, int, etc) you can exchange javascript function too.

for example:

<script language = "JavaScript">
<!--

var myJSONObject5 =
{
"test2": function (arg){ alert('This is a argument : '+arg) }
}

//-->
</script>
<form>
test2() method of object myJSONObject5test2() Giving argument 'hello', it executes<br>
<input type = "button"
onclick = "myJSONObject5.test2('hello')"
value = "click"><br><br>
</form>

So you can according to different user's preference, giving different javascript action.

for example:

var http_request = new XMLHttpRequest();
var url = "http://example.net/testingServlet?parameter1=TEST";

http_request.onreadystatechange = handle_json;
http_request.open("POST", url, true);
http_request.send(null);

in the above example, you can integrated with JAVA Servlet to return a JSON to client by using JSON in java API

JSMIN is a small program to minized javascript file by removing comments and return characters.