This is a migrated thread and some comments may be shown as answers.

Reading JSON from a file doesn't work...

3 Answers 138 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Gabriel
Top achievements
Rank 1
Gabriel asked on 17 Dec 2011, 07:50 AM
I'm trying to get a datasource to load a file that contains JSON data. This is based off the Demos on the site.
Here is how I create and load the datasource:
<script>
$(document).ready(function() {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "filters.json",
                dataType: "json"
            }
        },
        change: function() {
            // subscribe to the CHANGE event of the data source// do stuff here...
        });
    // read data from the "movies" array
    dataSource.read();
    });
</script>

And here is the filter.json file:
[{id:"oflc",title:"Classification",items:[{title:"root",values:"CTC,G,PG,M,MA,R"}]},{id:"drwho",title:"Favourite Doctor",items:[{title:"Classic",values:"William Hartnell,Patrick Troughton,Jon Pertwee,Tom Baker,Peter Davison,Colin Baker,Sylvester McCoy,Paul McGann"},{title:"2005+",values:"Christopher Eccleston,David Tennant,Matt Smith"},{title:"Non-canon",values:"Peter Cushing"}]},{id:"q",title:"Search term",items:[{title:"root",values:"ANY"}]},{id:"format",title:"Format",items:[{title:"root",values:"3D Blu-ray,Blu-ray,DVD"}]},{id:"companion",title:"Favourite Companion",items:[{title:"Classic",values:"Mary Tamm_Lala Ward[Romana],John Leeson[K9],Nicola Bryant[Peri],Katy Manning[Jo Grant]"},{title:"2005+",values:"Karen Gillan[Amy Pond]"}]},{id:"price",title:"Price",items:[{title:"root",values:"<20[Under $20]"}]}]

I've have removed stuff out that is not needed to illustrate the problem. The datasource seems to be blank and does not contain any date.

When I copy the data from the file and dump it in code as a variable and tell the datasource to use it, it works fine. Telling it to read from the file however seems hopeless. I've compared it to the samples and read the docos (not very useful BTW) and am stuck.

The JSON data is fine in the file and works with other things (non-Kendo). I'm using IE9.

3 Answers, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 19 Dec 2011, 09:38 AM
Hello Gabriel,

 The problem is that jQuery itself fails to parse the provided JSON data. The JSON parser is more restrictive compared to adding the data in the script - object keys need to be quoted, for example. 

I have created a sample project to demonstrate the error occurring using vanilla jQuery Ajax request for the provided JSON.

Best wishes,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gabriel
Top achievements
Rank 1
answered on 20 Dec 2011, 11:26 PM
Thanks - got it working now.
0
Petyo
Telerik team
answered on 21 Dec 2011, 07:58 AM
Hello Gabriel,

Forgot to mention - there are plenty of tools (JSONLint for instance) which can assist with JSON format errors. 

Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Gabriel
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Gabriel
Top achievements
Rank 1
Share this question
or