1. #1
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with more than one root

    JSON with more than one root


    Guys, I need your help on this...
    given this json data.

    {
    'results': '2'
    ,'hello':{'timeslots':
    [{ 'time' : '8:00am-9:00am'
    ,'tasks':
    [{ 'id': '1', 'task1': 'SR-AO,SR-account opening', 'task2': 'SR-AO,SR-account opening' }]

    }]
    }

    }

    how can you map id when tasks is inside hello root? please help me on this one guys, I need to display it on the grid

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    91
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    You'll have to parse your own JSON, e.g.
    Code:
    var reader = new Ext.data.JsonReader({
    	fields: ['time', 'id', 'task1', 'task2']
    });
    reader.readRecords = function(o){
    	this.jsonData = o;
    	var success, records = [], totalRecords;
    	totalRecords = parseInt(o.results, 10);
    	var root1 = o.hello.timeslots;
    	for(var i = 0, ilen = root1.length; i < ilen; i++){
    		var r1 = root1[i], root2 = r1.tasks;
    		for(var j = 0, jlen = root2.length; j < jlen; j++){
    			var r2 = root2[j];
    			var record = new this.recordType({
    				time: r1.time,
    				id: r2.id,
    				task1: r2.task1,
    				task2: r2.task2
    			}, r2.id); // <- assuming the id is unique
    			records.push(record);
    		}
    	}
    	return {
    		success : success !== undefined ? success : true,
    		records : records,
    		totalRecords : totalRecords !== undefined ? totalRecords : r.length
    	};
    };

  3. #3
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with more than one root

    JSON with more than one root


    Sir condor, I tried the code you gave me, but nothing happend, in firefox there's no error shown in firebug, but at the same time nothing was shown in the grid. Can you please take a look at my code and tell me if I did something wrong, I really appreciate your help..
    <code>
    var
    proxy =new Ext.data.HttpProxy({

    url
    : GLOBAL_URL_WEEKLY_CALENDAR
    });


    var myReader =new Ext.data.JsonReader({

    fields
    :['time','id','task1','task2'],

    readRecords
    :function(o){
    this.jsonData = o;

    var success, records =[], totalRecords;

    totalRecords
    = parseInt(o.results,10);

    var root1 = o.hello.timeslots;

    for(var i =0, ilen = root1.length; i < ilen; i++){

    var r1 = root1[i], root2 = r1.tasks;

    for(var j =0, jlen = root2.length; j < jlen; j++){

    var r2 = root2[j];

    var record =newthis.recordType({

    time
    : r1.time,

    id
    : r2.id,

    task1
    : r2.task1,

    task2
    : r2.task2
    }, r2.id);// <- assuming the id is unique

    records
    .push(record);

    }

    }

    return{

    success
    : success !== undefined ? success :true,

    records
    : records,

    totalRecords
    : totalRecords !== undefined ? totalRecords :0

    };

    }
    });



    var store =new Ext.data.Store({

    proxy
    : proxy,

    reader
    : myReader
    });


    // create the grid

    //gridone




    var week_grid =new Ext.grid.GridPanel({

    store
    : store
    ,autoHeight:true

    ,id:'grdweek'

    ,autoScroll:false

    ,width:'100%'

    ,sortable:false

    ,columns:[{

    header
    :"Day1",

    dataIndex
    :'time',

    sortable
    :false

    },{

    header
    :"tasks",

    dataIndex
    :'id',

    sortable
    :false

    }
    ]

    ,height:205

    ,trackMouseOver:false

    ,cls:'x-grid-row'

    ,disableSelection:false


    });

    store.load();
    </code>

    Thanks again in advance sir

  4. #4
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    91
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Sorry, that should have been:
    Code:
    var myReader = new Ext.data.JsonReader({
    	fields: ['time', 'id', 'task1', 'task2']
    });
    myReader.readRecords = function(o){
    	this.jsonData = o;
    	var success, records = [], totalRecords;
    	totalRecords = parseInt(o.results, 10);
    	var root1 = o.hello.timeslots;
    	for(var i = 0, ilen = root1.length; i < ilen; i++){
    		var r1 = root1[i], root2 = r1.tasks;
    		for(var j = 0, jlen = root2.length; j < jlen; j++){
    			var r2 = root2[j];
    			var record = new this.recordType({
    				time: r1.time,
    				id: r2.id,
    				task1: r2.task1,
    				task2: r2.task2
    			}, r2.id); // <- assuming the id is unique
    			records.push(record);
    		}
    	}
    	return{
    		success: success !== undefined ? success : true,
    		records: records,
    		totalRecords: totalRecords !== undefined ? totalRecords : r.length
    	};
    };
    ps. Is the id unique? Otherwise you need to remove the second parameter (r2.id) so Ext can auto-generate a unique id.

  5. #5
    Ext User
    Join Date
    Sep 2008
    Posts
    25
    Vote Rating
    0
    Rob Wilson is on a distinguished road

      0  

    Default


    Just a thought... an alternative approach (if you're in control of what generates the JSON object) is to alter the structure so you have a single root and the two children end up being your current two root elements.

  6. #6
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with more than one root

    JSON with more than one root


    Thank you very much sir condor and sir Rob. I really appreciate it and for other users of Ext that will encounter this problem I also found another way of doing it.

    after asigningning the root on the outside
    ..
    root:'1stroot'
    id:'id'
    ..

    try this in your mapping syntax:

    {
    name:'Activity1', mapping:'2ndroot', convert:function(v){return v[0].'item';}

    }

    Happy coding everyone...

    Thanks again sir condor and sir rob

  7. #7
    Ext User
    Join Date
    Sep 2008
    Posts
    25
    Vote Rating
    0
    Rob Wilson is on a distinguished road

      0  

    Default


    Now that's nice and compact, thanks for the tip!

  8. #8
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    91
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Your solution will result in x records (where x is the number of objects in the 1st root).
    I thought you wanted x*y records (where x is the number of objects in the 1st root and y the number of objects in the 2nd root).

    ps. You don't need a convert function. A simple mapping will also work:
    Code:
    root: 'hello.timeslots',
    fields: [
      'time',
      {name:'id', mapping: 'tasks[0].id'},
      {name:'task1', mapping: 'tasks[0].task1'},
      {name:'task2', mapping: 'tasks[0].task2'}
    ]

  9. #9
    Sencha User
    Join Date
    Mar 2011
    Posts
    3
    Vote Rating
    0
    kibriame is on a distinguished road

      0  

    Default


    Dear Condor ,
    I have facing the problem for multiple json root. I got the result from database but didn't retrieve some root data . here is the query result :
    ({
    "total":"13",
    "veh_cond_data":[{"veh_cond":"New","counter":"8"},{"veh_cond":"Recondition","counter":"3"},{"veh_cond":"Used","counter":"2"}],
    "color_data":[{"color":"red","counter":"5"},{"color":"silver","counter":"8"}],
    "data":[{"id":"206","user":null,"pro_name":"TOYOTA-COROLLA-X-2006_CB9162900368683040","veh_cond":"Used"},
    {"id":"198","user":null,"pro_name":"TOYOTA-ALLION--2011_CB9162900368683040","veh_cond":"New"}]
    })
    I have no problem to retirve data from data array and total, but i face problem in this two array,
    "veh_cond_data":[{"veh_cond":"New","counter":"8"},{"veh_cond":"Recondition","counter":"3"},{"veh_cond":"Used","counter":"2"}],
    "color_data":[{"color":"red","counter":"5"},{"silver":"8","counter":"3"}],

    How do I access data in "color_data" and "veh_cond_data" array in js file?
    Please give me some advice