将mongodb json绑定到剑道网格

时间:2015-11-26 04:56:00

标签: kendo-ui kendo-grid

我对kendo UI非常新,并且遇到类似的挑战(除了我不介意为网格设置固定的数据结构):

How to pass MongoDb Json value to KendoUI grid using webservice method

$("#grid").kendoGrid({

        dataSource: {
        transport: {
            read: {
                url: "http://localhost:8080/urlPath",
                dataType: "json",
               }
            },
    schema: {
            data: "score"
        }
    columns:[{
        field: "physics"
    },
    {
        field: "chemistry",
    }],
   });

Json看起来像:

[{"score"[{"physics:99", "chemistry" :95}]},{"score"[{"physics:99", "chemistry" :95}]}]

我在过去的几天里一直在努力,并尝试过像:

这样的方法

Convert dbcursor object into json

http://www.codeconfuse.com/2014/03/mongodb-convert-data-getting-from.html

以下是上述网址中的相关代码:

    while(cursor.hasNext()) { 
        BasicDBObject obj = (BasicDBObject) cursor.next();
        jsonobj = new JSONObject();
        BasicDBList name = (BasicDBList) obj.get("Name");
        jsonobj.put("Incident Date", obj.getString("Incident Date"));
        jsonarray.put(jsonobj);
  }
  return jsonarray;
JSON json =new JSON();
String serialize = json.serialize(cursor);

但是剑道网格似乎拒绝了它。请协助。

1 个答案:

答案 0 :(得分:0)

我的问题通过格式化以适当格式返回的json来解决,如下所示:

自:

[{"score"[{"physics:99", "chemistry" :95}]},{"score"[{"physics:99", "chemistry" :95}]**}]**

要:

[{"physics:99", "chemistry" :95}]},{"score"[{"physics:99", "chemistry" :95}]

所以,基本上我提取了得分对象,放入我自己的hashmap,然后返回json对象。请参阅下面的示例代码:

*

DB db = mongoClient.getDB( "test" );
DBCollection coll = db.getCollection("mycol");
BasicDBObject fields = new BasicDBObject("score",true).append("_id",false);
BasicDBObject query = new BasicDBObject();

DBCursor cursor = coll.find(query,fields)

while (cursor.hasNext()) {
    DBObject obj = cursor.next();
    BasicDBList scoreList = (BasicDBList) obj.get("score");

for (int i = 0; i < scoreList.size(); i++) {
        BasicDBObject scoreObj = (BasicDBObject) scoreList.get(i);
        String physics = scoreObj.getString("physics");
        String chemistry = scoreObj.getString("chemistry");
        //ofcourse we need another innerloop here to iterate through
        //all the rows of the returned list.   
        innerMap.add("physics",physics);
        innerMap.add("chemistry",chemistry);
    }       
return new Gson().gson(map.values);
}

*