Conversion between JSON and JAVA (struts Ajax)

JSON (JavaScript Object Notation) is a lightweight data interchange format, which is based on a subset of JavaScript, often used with ajax, using ajax to load data.
json ajax end of the configuration steps:
1. Guide package. Using json jar package of support needed, use the jar before the first import the following package: commons-beanutils-1.8.0.jar, commons-collections-3.2.1.jar (who did not import this package as a waste One morning my time to check for errors, Khan), commons-lang-2.4.jar, commons-logging-1.1.jar, ezmorph-1.0.6.jar, json-lib-2.3-jdk15.jar, jsonplugin-0.32 . jar;
2. Add JsonResultType class.
3. Configuration struts.xml profile. In the case of no use to use json, struts.xml the package configuration file inheritance is "struts-default" that is: extens = "struts-default", but when using the json inheritance no longer struts-default, but "json-default", open jsonplugin-0.32.jar under the struts-plugin.xml file you can see, the package has inherited the "struts-default". in the package by adding the following code
<result-types>
<Result-type name = "json"
"JsonResultType where the package">
</ Result-type>
</ Result-types>
Action in the appropriate configuration to do the following,
<action name="jsonAction""action Package ">
<result name="json" type="json">
<param name="name"> json </ param>
</ Result>
</ Action>
4. To write action code, (note the following code may not be prepared for any errors, please indicate)
private String json; / / need to get / set methods
/ / Get the collection may not necessarily be a collection of city
List list = this.service.method ();

/ / Filters do not need to convert the property
JsonConfig con = new JsonConfig ();
con.setExcludes (new String [] ("filter properties"));

/ / The list into JSONArray
JSONArray array = new JSONArray ();

/ / The array into a String
this.json = array.toString ();

/ / Is the action in the name struts.xml
return "json";
5.javascript call
<script type="text/javascript">
function method () (
/ / Request Action
var url = "jsonInfo / jsonAction! showCity.action";
/ / Ajax call
var ajax = new Ajax.Request (url, (
method: 'post',
onComplete: function (data) (
/ / Here for the city is a string, according to the corresponding conversion needs to be done
var city = JSON.parse (data.responseText);
)
));
)
</ Script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值