-
12 Jan 2009 12:16 AM #1
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
-
12 Jan 2009 12:50 AM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
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 }; };
-
12 Jan 2009 2:23 AM #3
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>
varproxy =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
-
12 Jan 2009 3:31 AM #4Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
Sorry, that should have been:
ps. Is the id unique? Otherwise you need to remove the second parameter (r2.id) so Ext can auto-generate a unique id.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 }; };
-
12 Jan 2009 4:17 AM #5
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.
-
12 Jan 2009 5:37 PM #6
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
-
13 Jan 2009 12:38 AM #7
Now that's nice and compact, thanks for the tip!

-
13 Jan 2009 12:44 AM #8Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
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'} ]
-
11 Aug 2012 1:25 AM #9
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


Reply With Quote