【问题标题】:Interpreting JSON to Kendo UI Mobile Listview将 JSON 解释为 Kendo UI Mobile Listview
【发布时间】:2013-04-24 02:38:17
【问题描述】:

我正在尝试将我的 JSON 数据转换为 Kendo UI Mobile Listview。我的 php 脚本输出这些数据:

[{"eventID":"1","name":"test","time":"12:00:00","category":"####","subcategory":"####","description":"Event for testing purposes","locationID":"1","picturePath":"http:\/\/####\/~z3370257\/images\/1.jpg"},{"eventID":"2","name":"test2","time":"13:00:00","category":"####","subcategory":"SEIT","description":"Event for testing purposes2","locationID":"1","picturePath":"http:\/\/####\/~z3370257\/images\/1.jpg"}]

This JS fiddle 使用与我的应用相同的 html css 和 javascript 文件。

我的问题是,我需要在我的传输和读取方法中添加什么才能正确解释数据。

【问题讨论】:

  • 你确定你得到的是 json 吗?您发布的数据不是 JSON。
  • 是的,我有 99.99999% 的把握,那就是调试器解释我正在获取的数据。 php 脚本回显 json 编码数据。
  • 你确定你变成的数据是一个数组吗?你能尝试用你的数据制作一个 jsFiddle 吗? (可以修复编码,没关系!)
  • 请完全按照您从 fiddler 或您的浏览器开发工具捕获的数据发布数据。我们还需要确定 .000001% ;)
  • 我已经修复了我的 php 并更新了问题以反映新信息,有人有任何想法/帮助吗?

标签: jquery ajax json kendo-ui kendo-mobile


【解决方案1】:

您的剑道代码没问题。只能有 2 个问题:

  1. 服务返回的数据不是正确的 JSON。它应该像这样编码:

                    [{ 
                    "category": "123",
                    "description": "Event for testing purposes",
                    "eventID": "1",
                    "locationID": "1",
                    "name": "Kendo",
                    "picturePath": "https://si0.twimg.com/profile_images/1514396238/KendoUI-Figure.png",
                    "subcategory": "SEIT",
                    "time": "12:00:00"
                } ,
    
                {
                    "category": "123",
                    "description": "Event for testing purposes",
                    "eventID": "1",
                    "locationID": "1",
                    "name": "jQuery",
                    "picturePath": "http://dochub.io/images/jquery_logo.png",
                    "subcategory": "SEIT",
                    "time": "12:00:00"
                }]
    
  2. 您的 DataSource 已在函数“dataInit”中初始化,该函数未在代码中的任何位置显示。并且 listview 在视图的 data-init 事件中被初始化。所以我假设列表在数据源之前初始化并导致数据不被绑定。 解决此问题的方法是,您可以将 DataSource 保留在 dataInit 函数之外,如此小提琴所示,它可以按照您想要的方式使用您的代码:http://jsfiddle.net/whizkid747/MPzVu/

    var app = new kendo.mobile.Application(document.body);        
    var dataSource = new kendo.data.DataSource({
    
        transport: {
            read: {
                //using jsfiddle echo service to simulate JSON endpoint
                url: "/echo/json/",
                dataType: "json",
                type: "POST",
                data: {
                    // /echo/json/ echoes the JSON which you pass as an argument
                    json: JSON.stringify([
                        {
                            "category": "123",
                            "description": "Event for testing purposes",
                            "eventID": "1",
                            "locationID": "1",
                            "name": "Kendo",
                            "picturePath": "https://si0.twimg.com/profile_images/1514396238/KendoUI-Figure.png",
                            "subcategory": "SEIT",
                            "time": "12:00:00"
                        } ,
                        {
                            "category": "123",
                            "description": "Event for testing purposes",
                            "eventID": "1",
                            "locationID": "1",
                            "name": "jQuery",
                            "picturePath": "http://dochub.io/images/jquery_logo.png",
                            "subcategory": "SEIT",
                            "time": "12:00:00"
                        }
                    ])
                }
            }
        }
    });
    
    function loadEventNames(){           
      $("#eventfeed").kendoMobileListView({
      dataSource: dataSource,
      template: $("#eventNameTemplate").html(),
      style:'inset'
     });
    }
    

【讨论】:

  • 我从服务器得到的响应是:{"eventID":"1","name":"test","time":"12:00:00","category": "####","subcategory":"SEIT","description":"用于测试目的的事件","locationID":"1","picturePath":"http:\/\/####\ /~####\/images\/1.jpg"} 我的IDE(Icenium)只是把它放在我的桌子上。 dataInit 也在 onDeviceReady() 函数中运行,我尝试自行关闭它,但它仍然无法正常工作。
  • 这个小提琴对你有帮助吗? jsfiddle.net/whizkid747/MPzVu 。您的解决方案与小提琴有何不同?
  • 是的,我试图精确地复制它,即使在我放入我的 php url 的第二秒使用实际的小提琴而不是 echo thingy 它得到了永远的加载问题。我的 php 脚本看起来像这样。 jsfiddle.net/F5BFY(不适合放在评论框内)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
相关资源
最近更新 更多