日期:2014-05-17  浏览次数:20791 次

Extjs解析struts2返回的json数据错误求大虾给指点一下 谢谢啦。(分不多了,不好意思)。很急急急的
我用的是struts2.1.8的struts2-json-plugin-2.1.8.1.jar插件 还有json-lib-2.4-jdk15.jar

java代码就是提供set和get方法
Java code

int count = fieldManageService.getFieldmanageCount(name);
List<Fieldmanage> list = fieldManageService.getFieldmanageList(name, start, limitNo);
JSONArray array = JSONArray.fromObject(list);
System.out.println(array.toString()+"dsds111sdsd");
this.setData(array.toString());
this.setTotalCount(count);



xml配置
XML code

        <action name="fieldManage_*" method="{1}" class="fieldManageAction">
        <interceptor-ref name="json"></interceptor-ref>
        <result type="json">
            <param name="includeProperties">totalCount,data</param>
        </result>
        </action>



前台的Ext
JScript code

        this.vstore = new Ext.data.JsonStore({
            url:this.listUrl,
            totalProperty : 'totalCount',
            root : 'data',
            fields:this.fields,
        })
this.grid = new Ext.grid.GridPanel({
            store:this.vstore,
            loadMask: {msg:'正在加载数据,请稍侯……'},
            tbar:[
                {
                    text:"添加",
                    iconCls:"addIcon",
                    handler:this.addActivity,
                    scope:this
                },
                    "-",
                {
                    text:"修改",
                    iconCls:"editIcon",
                    handler:this.editActivity,
                    scope:this
                },
                    "-",
                {
                    text:"删除",
                    iconCls:"deleteIcon",
                    handler:this.delActivity,
                    scope:this
                }
                ],
            frame:true,
            columns:this.getColumns(),
            bbar:this.pagingToolbar
        })
        


 
前台FF检测到的json是[{"fieldManageDesc":"海都餐厅门口","fieldManageId":1,"fieldManageName":"餐厅门口","isuse":"1"}]是对的、但是Ext解析的时候应该是一条、但是他把这个串拆分开了、拆成85条了,例如第一条就是一个“[”,第二个就是“{”,一次类推、我很纳闷了。。

------解决方案--------------------

仍然用这个配置
<action name="fieldManage_*" method="doSomeThing" class="fieldManageAction">
<interceptor-ref name="json"></interceptor-ref>
<result type="json">
//后边这个配置不要了,默认把类成员全转成json对象
//<param name="includeProperties">totalCount,data</param>
</result>
</action>
方法名改成了 doSomeThing ,以此为例

//类名不变
public class fieldManageAction
{
//定义类成员,需要有get和set方法。 有get方法的就会自动转换到返回的json对象里
//除非在参数里用 excludeProperties 排除或是在类里用@JSON(serialize=false) 排除
//如果是Spring 自动注入的,一定不要给get方法或是一定要把它排除
private int totalCount; //整型
private Date today; //日期
private String memo; //字符串
private List<Fieldmanage> list; //对象 也是自动转换
//以上都给 get 和 set 方法


//这是ACTION 方法,方法里只需要对要返回的类成员赋值就行了
public String doSomeThing()
{
//……………………调用逻辑层方法处理逻辑 
doBusinessThing();