struts2 JSON Plugin

  What's JSON??
  json is JavaScript Object Notation,is a lightweight syntax for representing data,JSON and XML is both for data transfer of AJAX.
  1.SON Doesn't Have Namespaces.  But every object is a namespace. Its set of keys is independent of all other objects, even exclusive of nesting. Also, JSON uses context to avoid ambiguity, just as programming languages do.
  2.JSON Has No Validator. Ultimately, every application is responsible for validating its inputs. This cannot be delegated. A YAML validator could be used however.
  3.JSON Is Not Extensible. It doesn't need to be. JSON is flexible. It can represent any non-recurrent data structure as is.New fields can be added to existing structures without obsoleting existing programs.
  4.JSON Is Not XML. Douglas argued that JSON is much simpler than XML.
  Let's seem what difference about JSON and XML:
xml 代码
  1. xml version='1.0' encoding='UTF-8'?>  
  2. <card>  
  3.    <fullname>Sean Kelly<!---->fullname>  
  4.    <org>SK Consulting<!---->org>  
  5.    <emailaddrs>  
  6.       <address type='work'>[email protected]<!---->address>  
  7.       <address type='home' pref='1'>[email protected]<!---->address>  
  8.    <!---->emailaddrs>  
  9.    <telephones>  
  10.       <tel type='work' pref='1'>+1 214 555 1212<!---->tel>  
  11.       <tel type='fax'>+1 214 555 1213<!---->tel>  
  12.       <tel type='mobile'>+1 214 555 1214<!---->tel>  
  13.    <!---->telephones>  
  14.    <addresses>  
  15.       <address type='work' format='us'>1234 Main St  
  16.          Springfield, TX 78080-1216<!---->address>  
  17.       <address type='home' format='us'>5678 Main St  
  18.          Springfield, TX 78080-1316<!---->address>  
  19.    <!---->addresses>  
  20.    <urls>  
  21.       <address type='work'>http://seankelly.biz/<!---->address>  
  22.       <address type='home'>http://seankelly.tv/<!---->address>  
  23.    <!---->urls>  
  24. <!---->card>  
With JSON, it looks like this:
js 代码
  1. {  
  2.    "fullname""Sean Kelly",  
  3.    "org""SK Consulting",  
  4.    "emailaddrs": [  
  5.       {"type""work""value""[email protected]"},  
  6.       {"type""home""pref": 1, "value""[email protected]"}  
  7.    ],  
  8.     "telephones": [  
  9.       {"type""work""pref": 1, "value""+1 214 555 1212"},  
  10.       {"type""fax""value""+1 214 555 1213"},  
  11.       {"type""mobile""value""+1 214 555 1214"}  
  12.    ],  
  13.    "addresses": [  
  14.       {"type""work""format""us",  
  15.        "value""1234 Main StnSpringfield, TX 78080-1216"},  
  16.       {"type""home""format""us",  
  17.        "value""5678 Main StnSpringfield, TX 78080-1316"}  
  18.    ],  
  19.     "urls": [  
  20.       {"type""work""value""http://seankelly.biz/"},  
  21.       {"type""home""value""http://seankelly.tv/"}  
  22.    ]  
  23.  
  24. }  
As you can see, JSON has structure with nesting of data elements, just as XML does.
  
  Use struts2 JSON plugin:
The struts2 JSON plugin provides a "json" result type can serializes actions into JSON.u can download it from here: http://code.google.com/p/jsonplugin/downloads/list

Write the mapping for the action:
 
xml 代码
  1. xml version="1.0" encoding="UTF-8" ?>  
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4.   
  5. <struts>  
  6.   
  7.   <package name="example"  extends="json-default">  
  8.      <action name="JSONExample" class="example.JSONExample">  
  9.         <result type="json"/>  
  10.      <!---->action>  
  11.   <!---->package>  
  12.   
  13. <!---->struts>  
  Use the "root" attribute(OGNL expression) to specify the root object to be serialized.
xml 代码
 
  1. <result type="json">  
  2.   <param name="root">  
  3.     person.job  
  4.   <!---->param>  
  5. <!---->result>  
The "root" attribute(OGNL expression) can also be used on the interceptor to specify the object that must be populated, make sure this object is not null.
xml 代码
 
  1. <interceptor-ref name="json">  
  2.   <param name="root">bean1.bean2<!---->param>  
  3. <!---->interceptor-ref>  
also ,u can use param 'excludeProperties' to exclude the JSON result set what u don't need:
xml 代码
 
  1. <result type="json">  
  2.   <param name="excludeProperties">  
  3.       bean1,bean2,bean3  
  4.   <!---->param>  
  5. <!---->result>  

你可能感兴趣的:(json,xml,Ajax,struts,mobile)