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

JSON parsing issues in Kendo DataSource

3 Answers 526 Views
Report a bug
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrew
Top achievements
Rank 1
Andrew asked on 18 Dec 2013, 04:13 AM
I am trying to use Kendo/Icenium to develop an app that talks to one or more existing Web backends, and am finding the Kendo DataSource to be brittle/buggy.  I am using the Icenium Kendo Project boilerplate, making minor modifications to the weather.json file to reproduce more complex issues I have found.

1. kendo.data.DataSource can only be used for JSON results that are of type array.  If the target service returns a single dictionary with embedded results, DataSource can't be made to deal with it.  Perhaps there is a workaround?  This can be reproduced by enclosing the array in weather.json in a dictionary that sets the array as its "results:" key, as in: { "results": [ ... ] }.  How do you display this?
2. kendo/Icenium fails to log anything to the console if the returned JSON document is invalid.  Simply insert an extra comma or remove an existing comma anywhere in the document.  The spinner will spin forever and there is no feedback to the console.
3. The JSON parsing fails even on a perfectly legal JSON document if one of the array items contains a nested dictionary with a parent key.  For example (again, slight mod to weather.json):
{
"day": "Monday",
"highTemperature": 28,
"lowTemperature": 14,
"image": "shower",
 "nested" : { "parent" : 0 }
}
This results in an exception from kendo.mobile.min.js:
Uncaught TypeError: Property 'parent' of object [object Object] is not a function

These issues are unfortunately making it difficult to get off the ground.  Any workarounds are appreciated.

3 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 20 Dec 2013, 05:32 PM
Hi Andrew,

Straight to your questions:

1. In order to load this data structure you should configure the schema.data property of the dataSource:
2. Loading animation is shown on requestStart and hidden on requestEnd event of the DataSource. RequestEnd will not be fired if the retrieved data is invalid hence the application will continue to display the loading animation. The developer should hook up to the error event of the DataSource and handle errors manually. If you would like to hide the animation this could be through the hideLoading method of the application:
3. "parent" is reserved field in kendo.data.ObservableObject (after parsing data items are transformed in observableObjects).
Using another name for the parameter will fix the issue. For example:
{
"day": "Monday",
"highTemperature": 28,
"lowTemperature": 14,
"image": "shower",
"nested" : { "parent1" : 0 }
}

On a side note the DataSource is not designed to work with nested data structures. Although that it is possible to parse and load the "nested" object some functionality such as filtering might not work.

I hope this information will help.

Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Andrew
Top achievements
Rank 1
answered on 20 Dec 2013, 05:56 PM
Thanks for the thorough answers, Alexander!

The answer to #3 is a disappointment.  In this case I'm using the WordPress REST JSON API:
http://wordpress.org/plugins/json-rest-api/
and it inserts a "parent" field at multiple levels in the JSON.  So it sounds like Kendo cannot be used to create apps for this particular REST API.

Is there a hook that would allow the JSON to be pre-processed to remove/rename any "parent" keys before the observable is created?
0
Accepted
Andrew
Top achievements
Rank 1
answered on 20 Dec 2013, 07:44 PM
Ahh--studied further in the docs, thanks for the pointer.  I need to replace the "read" dictionary in the data source definition with a function that does the same.  This also allows me to put a hook for parser error.

                    read: function(options) {
                        console.log("Requesting posts");
                      // make JSONP request to http://demos.kendoui.com/service/products
                      $.ajax({
                          
                        url: "http://gear11.com/wp-json.php/posts",
                        dataType: "json", 
                        success: function(result) {
                          console.log("Success response");
                          console.log(result);
                          // notify the data source that the request succeeded
                          data = utils.filter(['parent'], result);
                          options.success(data);
                        },
                        error: function(result, e) {
                          console.log("Error response: ");
                          console.log(e);
                          // notify the data source that the request failed
                          options.error(result);
                        }
                      });
                    }
Tags
Report a bug
Asked by
Andrew
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or