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

Working with complex JSON objects

4 Answers 347 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Chandu
Top achievements
Rank 1
Chandu asked on 22 Mar 2012, 03:31 AM
I have a JSON object that is returned from the WS which will have a sample of below:
 
{
    "MenuID"5,
    "MenuVersion"1,
    "MenuName""Lunch Menu",
    "MenuItems"[
        {
        "Name""TUSCANI MEDITERRANEAN CON POLLO",
        "Description""Pasta",
        "PKID"2,
        "ParentID"1,
        "Ingredients"[
            {
            "PKID"123,
            "IngName""Cheese",
            "Included"true,
            "ExtraPrice"0},
        {
            "PKID"124,
            "IngName""Sausage",
            "Included"false,
            "ExtraPrice"0.99}
        ],
        "ItemPricing"[
            {
            "PKID"456,
            "SizeName""Large",
            "SizePrice"12.99},
           {
            "PKID"678,
            "SizeName""Small",
            "SizePrice"14.99}
        ]}
    ]
}‚Äč 

Are there any examples on how to work with complex objects like these. (Loop through the Menu items in this example and being able to display required info.) . I need to dynamically display the prices or ingredients to the user based on what is selected.  I will be having more than one Menu Item to work with.

Thanks,
Chandu

4 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 23 Mar 2012, 01:48 PM
Hello Chandra,

I am afraid the described scenario is not supported at the moment - the dataSource does not work with hierarchical objects.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Andre
Top achievements
Rank 1
answered on 25 Mar 2012, 04:52 AM
Hi Chandu

The way I got around this was to parse my incoming data in the, schema parse: section of the datasource, so parse it to a string and then use that string, not perfect but worked for my case.
0
Chandu
Top achievements
Rank 1
answered on 25 Mar 2012, 12:08 PM
Andre,

Thanks for the suggestion, I have done something similar in a SPA when not using KendoUI. I am trying to migrate my work and having hard time deciding on whether going in KendoUI Datasource route or KO mvvm route. I want the ability to handle JSON objects as I mentioned in my initial post. 

You idea sounds good, do you have a simple example of how you implemented it so that I can learn from it rather than start from scratch.

Thanks,
Chandu
0
Andre
Top achievements
Rank 1
answered on 25 Mar 2012, 01:20 PM
This may help: 
I don't have a dump of my data handy, so it may look confusing, but will add it when i get to it again

var
timeArray = new kendo.data.DataSource({
      transport: {
          read: "/my/url"
      },
      schema: {
       parse: function(data) {
              $.each(data, function(i, val) {         
                  val.complexDataField =  getData(val);  
          });
       return data;
      }
      }
  });
 
// Descend into data and concatenate it
 getData = function(data) {
    var item = ' ';
    for(var i in data['from']) {
      if(!jQuery.isEmptyObject(data['rate'][i])) { var rate = "$" + data['rate'][i];}else{var rate = "";}
        item += data['from'][i] + ' ' + data['to'][i] + ' ' + rate + "";
      }
    return item
  }
Tags
Data Source
Asked by
Chandu
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Andre
Top achievements
Rank 1
Chandu
Top achievements
Rank 1
Share this question
or