Java 用JSON值填充ext网格时出现问题

Java 用JSON值填充ext网格时出现问题,java,json,extjs,grid,Java,Json,Extjs,Grid,我是新手,有个问题:我正试图用数据填充extjs网格: Ext.onReady(function() { var store = new Ext.data.JsonStore({ root: 'topics', totalProperty: 'totalCount', idProperty: 'threadid', remoteSort: true, a

我是新手,有个问题:我正试图用数据填充extjs网格:

    Ext.onReady(function() {
        var store = new Ext.data.JsonStore({
            root: 'topics',
            totalProperty: 'totalCount',
            idProperty: 'threadid',
            remoteSort: true,
            autoLoad: true, ///

            fields: [
                'title', 'forumtitle', 'forumid', 'author',
                {name: 'replycount', type: 'int'},
                {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
                'lastposter', 'excerpt'
            ],

            proxy: new Ext.data.ScriptTagProxy({
            url:'http://10.10.10.101:8080/myproject/statusList/getJobs/2-10/search-jobname-/sort-asdf/filterjobname-123/filterusername-davs/filterstatus-completed/filtersubmdate-today',
            method : 'GET'
        })
    });

    //
    var cm = new Ext.grid.ColumnModel([
        {sortable:true, id : 'id',          dataIndex:'id'},
        {sortable:true, id : 'title',       dataIndex:'title'},
        {sortable:true, id : 'forumtitle',  dataIndex:'forumtitle'},
        {sortable:true, id : 'forumid',     dataIndex:'forumid'},
        {sortable:true, id : 'author',      dataIndex:'author'}
    ]);

    var grid = new Ext.grid.GridPanel({
        id: 'mainGrid',
        el:'mainPageGrid',
        pageSize:10,
        store:store,
        //    stripeRows: true,
        cm:cm,
        stateful: false, // skipSavingSortState
        viewConfig:{
            forceFit:true
        },
        //        width:1000,
        //        height:700,
        loadMask:true,
        frame:false,

        bbar: new Ext.PagingToolbar({
            id : 'mainGridPaginator',
            store:store,
            hideRefresh : true,
            plugins: new Ext.ux.Andrie.pPageSize({
                beforeText: 'View: ',
                afterText: '',
                addAfter: '-',
                variations: [10, 25, 50, 100, 1000]
                //comboCfg: {
                    //id: '${ dispview_widgetId }_bbar_pageSize'
                //}
            }),
            displayMsg: 'Displaying items {0} - {1} of {2}',
            emptyMsg:'No data found',
            displayInfo:true
        })
    });

    grid.render();
});
Java部分:

@GET
@Path("/getJobs/{startFrom}-{startTo}/search-{searchType}-{searchName:.*}/" +
        "sort-{sortType}/filterjobname-{filterJobName:.*}/filterusername-{filterUsername:.*}/" +
        "filterstatus-{filterStatus:.*}/filtersubmdate-{filterSubmittedDate:.*}")
@Produces({"application/json"})
@Encoded
public String getJobs(
        @PathParam("startFrom")             String startFrom,
        @PathParam("startTo")               String startTo,
        @PathParam("searchType")            String searchType,
        @PathParam("searchName")            String searchName,
        @PathParam("sortType")              String sortType,
        @PathParam("filterJobName")         String filterJobName,
        @PathParam("filterUsername")        String filterUsername,
        @PathParam("filterStatus")          String filterStatus,
        @PathParam("filterSubmittedDate")   String filterSubmittedDate) {


    return "{totalCount:'3',topics:[{title:'XTemplate with in EditorGridPanel',threadid:'133690',username:'kpremco',userid:'272497',dateline:'1305604761',postid:'602876',forumtitle:'Ext 3x Help',forumid:'40',replycount:'2',lastpost:'1305857807',lastposter:'kpremco',excerpt:'Hi I have an EditiorGridPanel whose one column i am using XTemplate to render and another Column is Combo Box FieldWhen i render the EditorGri'}," +
                                             "{title:'IFrame error _flyweights is undefined',threadid:'133571',username:'Daz',userid:'52119',dateline:'1305533577',postid:'602456',forumtitle:'Ext 3x Help',forumid:'40',replycount:'1',lastpost:'1305857313',lastposter:'Daz',excerpt:'For Ext 330 using Firefox 4  Firebug, the following error is often happening when our app loads e._flyweights is undefined Yetthis '}," +
                                             "{title:'hellllllllllllllpwhy it doesnt fire cellclick event after I change the cell value',threadid:'133827',username:'aimer311',userid:'162000',dateline:'1305700219',postid:'603309',forumtitle:'Ext 3x Help',forumid:'40',replycount:'3',lastpost:'1305856996',lastposter:'aimer311',excerpt:'okI will discribe this problem as more detail as I canI look into this problem for a whole dayI set clicksToEdit1 to a EditorGridPanelso when I'}]}";
因此,我得到一个JavaScript错误:

Syntax error at line 1 while loading:
totalCount:'3',topics:[{title:'XTemplate
---------------------^
expected ';', got ':'
但是,当我使用代理的URL时:

URL: 'http://extjs.com/forum/topics-browse-remote.php',
表示相同的信息,我没有任何问题

我的失败在哪里

p.S.对第一个答案的评论:

    return "{\"totalCount\":\"3\",\"topics\":[{\"title\":\"XTemplate with in EditorGridPanel\",\"threadid\":\"133690\",\"username\":\"kpremco\",\"userid\":\"272497\",\"dateline\":\"1305604761\",\"postid\":\"602876\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"2\",\"lastpost\":\"1305857807\",\"lastposter\":\"kpremco\",\"excerpt\":\"Hi I have an EditiorGridPanel whose one column i am using XTemplate to render and another Column is Combo Box FieldWhen i render the EditorGri\"}," +
                                             "{\"title\":\"IFrame error _flyweights is undefined\",\"threadid\":\"133571\",\"username\":\"Daz\",\"userid\":\"52119\",\"dateline\":\"1305533577\",\"postid\":\"602456\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"1\",\"lastpost\":\"1305857313\",\"lastposter\":\"Daz\",\"excerpt\":\"For Ext 330 using Firefox 4  Firebug, the following error is often happening when our app loads e._flyweights is undefined Yet, this \"}," +
                                             "{\"title\":\"hellllllllllllllpwhy it doesn't fire cellclick event after I change the cell value\",\"threadid\":\"133827\",\"username\":\"aimer311\",\"userid\":\"162000\",\"dateline\":\"1305700219\",\"postid\":\"603309\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"3\",\"lastpost\":\"1305856996\",\"lastposter\":\"aimer311\",\"excerpt\":\"okI will discribe this problem as more detail as I canI look into this problem for a whole dayI set clicksToEdit1 to a EditorGridPanelso when I\"}]}";
我有以下错误:

Syntax error at line 1 while loading:
{"totalCount":"3","topics":[{"title
-------------^
expected ';', got ':'
第2页。当我将“[”添加到响应字符串的开头,将“]”添加到响应字符串的结尾时,erros消失了,但网格中没有填充数据

您没有返回(有效的)JSON。有关详细信息,请参阅,但例如,所有属性键都必须使用双引号。(所有字符串也必须使用双引号;单引号对JSON字符串无效。)

例如,这不是有效的JSON:

{totalCount:'3'}
…因为键不在引号中,而值使用单引号。正确的JSON应该是:

{"totalCount":"3"}
…如果确实希望
3
为字符串,或:

{"totalCount":3}
…如果
3
应该是一个数字


人们经常混淆JSON和JavaScript的对象文字符号,但它们是不同的。具体来说,JSON是对象文字符号的子集。很多在对象文字符号中有效的东西在JSON中无效。任何时候您有疑问,都可以查看,它提供了一个合适的JSON验证器。

我已经找到了问题的根源

正如我所知,Ext使用参数'callback=[some_callback_name]'发送到我的web服务函数(例如callback1001)。这意味着Extjs不仅希望获得JSON格式的结果,还希望获得“callback1001()”格式的结果。当我以这种格式返回数据时,一切都变好了

证明链接:

  • (答复#6)


我已经在我的演讲结束时发表了评论question@davs:您仍然没有返回有效的JSON(您遗漏了一些引号,例如“主题”,可能还有其他问题)。因此,首先要确保返回的是有效的JSON。我怀疑还发生了其他事情,但这是第一步。非常感谢JSON验证器,我再次更改了返回字符串,但即使它说JSON是有效的,我也有问题…@davs:是的,你的最新更新现在是有效的JSON,所以至少你已经消除了这一点。但我仍然得到了错误“预期”,得到“:”(正如我在问题末尾所描述的)