PDA

View Full Version : Reading the Nested Json



gajanan.borde
23 Mar 2011, 2:56 AM
Q1)Please help me how can i read this nested Json returned by the server.The structure of the Json is
as follows:
Json
{
"DATA": {
"code": 1001,
"message": "success",
"data": {
"lunList": [
{
"Device": "sys_disk-1",
"Handler": "vdisk",
"Exclusive-lun": "0",
"Ports": "iqn.2008-05.com.nDisks:storage.target3",
"Group(s)": "Default-0"
},
{
"Device": "sys_disk-2",
"Handler": "vdisk",
"Exclusive-lun": "0",
"Ports": "iqn.2008-05.com.nDisks:storage.target3",
"Group(s)": "Default-1"
},
{
"Device": "sys_disk-3",
"Handler": "vdisk",
"Exclusive-lun": "0",
"Ports": "iqn.2008-05.com.nDisks:storage.target3",
"Group(s)": "Default-0"
},
{
"Device": "sys_disk-4",
"Handler": "vdisk",
"Exclusive-lun": "0",
"Ports": "iqn.2008-05.com.nDisks:storage.target3",
"Group(s)": "Default-1"
}
]
}
}
}

I am reading the Json in this way


function xyz(){

alert("inside XYZ");
var store1 = new Ext.data.JsonStore({
// /fields: ['Device', 'Handler','Exclusive-lun','Ports', 'Group(s)'],

reader: {
type : 'json',
root : 'DATA',
fields: [
{name: 'Device', mapping :' lunList.Device'}
// {name: 'account_number'},
//{name: 'detail', mapping: 'report_data.detail'}
]



},



proxy: new Ext.data.HttpProxy({
url: 'http://localhost:8080/Json/register'
}),
autoLoad: true
});



var grid = new Ext.grid.GridPanel({
store:store1,
columns: [
{
id:'id',
header: "Device",
width: 160,
sortable: true,
dataIndex: 'Device'
},

{
header: "Handler",
width: 75,
sortable: true,
dataIndex: 'Handler'
},

{
header: "Exclusivelun",
width: 85,
sortable: true,
dataIndex: 'Exclusivelun'
},

{
header: "Ports",
width: 75,
sortable: true,
dataIndex: 'Ports'
},

{
header: "Group",
width: 85,
sortable: true,
dataIndex: 'Group'
}


],
stripeRows: true,
autoExpandColumn: 'id',
height:350,
width:600,
title:'Array Grid'
});
grid.render('grid-example');


}

Please help me how can i read the nested Json?

abhijeetljoshi
9 Nov 2011, 2:43 AM
I am getting similar json response unabe to get the required information from mapping

var electronicDevice = Ext.data.Record.create([

{ name : 'label', mapping: 'fields.label'}, // mapping needed here because name != mapping
{ name : 'rawValue', mapping: 'fields.rawValue'}
]);

var reader = new Ext.data.JsonReader({
root : "data",
//totalProperty : "totalCount",
//id : "testid"
}, electronicDevice);

var opportunityDetailsStore = new Ext.data.Store({
nocache : true,
reader : reader,
autoLoad : true,
remoteSort : true,
proxy : new Ext.data.HttpProxy({
url : '/jsonopp?s=6003&id='+oppId,
method : 'GET'
}),
});
//Grid is populating with the headers but no data
var oppDetailGrid = new Ext.grid.GridPanel({
ds: opportunityDetailsStore,
height:200,
title:'title',
cm: new Ext.grid.ColumnModel([
{header : "Label", width: 100, dataIndex : 'label'},
{header: "rawValue", width: 100, dataIndex: 'rawValue'}
])
});



My JSON response is
{"totalCount":1,"data":[{"fields":[{"booleanValue":false,"creatable":false,"crmFieldType":"boolean","crmType":"","customField":false,"dateValue":null,"decimalValue":0,"fieldLength":0,"floatValue":0,"id":0,"integerValue":0,"label":"Deleted","name":"IsDeleted","rawValue":"false","required":false,"stringValue":"","updateable":false},{"booleanValue":false,"creatable":true,"crmFieldType":"string","crmType":"","customField":false,"dateValue":null,"decimalValue":0,"fieldLength":18,"floatValue":0,"id":0,"integerValue":0,"label":"Account ID","name":"AccountId","rawValue":"00190000008r4vAAAQ","required":false,"stringValue":"00190000008r4vAAAQ","updateable":true},{"booleanValue":false,"creatable":false,"crmFieldType":"decimal","crmType":"","customField":false,"dateValue":null,"decimalValue":1533,"fieldLength":0,"floatValue":0,"id":0,"integerValue":0,"label":"Expected Amount","name":"ExpectedRevenue","rawValue":"1533.0","required":false,"stringValue":"","updateable":false}]}],"success":true}

Thanks for the help in advance.

gajanan.borde
6 Mar 2012, 4:39 AM
I resolved this issue as suppose your json like,

{"data":{"name":"xtz"}}

then you can read it as data.name.

similarly you can load data into grid also by specifying root element simillarly