PDA

View Full Version : Random Field names in JSON Data



fujy
7 Nov 2012, 3:16 AM
Hello,

I have a problem with JSON response data that I don't how to model it
Here's an example

If the JSON data is something like this:



{
"users" : [
{
"name" : "tom",
"state" : "WA",
"id" : "cedf-c56f-18a4-4b1"
},
{
"name" : "steve",
"state" : "NY",
"id" : "ebb2-92bf-3062-7774"
},
{
"name" : "mike",
"state" : "MA",
"id" : "fb60-b34f-6dc8-aaf7"
},
.
.
.
]
}


I know that the model of that data will be something like this



Ext.define('Foo.model.User',{
extend: 'Ext.data.Model',

fields: [
{name: 'name' ,type: 'string'},
{name: 'state' ,type: 'string'},
{name: 'id' ,type: 'string'},
],
});


But what if the JSON response data are like these
Note: I don't have any control on the response



{
"users" : {
"cedf-c56f-18a4-4b1" : {
"name" : "tom",
"state" : "WA",
"id" : "cedf-c56f-18a4-4b1"
},
"ebb2-92bf-3062-7774" : {
"name" : "steve",
"state" : "NY",
"id" : "ebb2-92bf-3062-7774"
},
"fb60-b34f-6dc8-aaf7" : {
"name" : "mike",
"state" : "MA",
"id" : "fb60-b34f-6dc8-aaf7"
},
.
.
.
}
}


And of course the "cedf-c56f-18a4-4b1", "ebb2-92bf-3062-7774" ... etc, are totally unknown to me

How can I make the Model, Reader, ... etc for this data?

Thanks in advance

Edit: The field names that cause the problem (e.g. "cedf-c56f-18a4-4b1") are embedded in the data

tvanzoelen
7 Nov 2012, 4:46 AM
Better then to return metadata in the response, and decode the response



var obj = Ext.decode(r.responseText);


From the MetaData create a new Store and reconfigure the grid



grid.reconfigure(newStore)


Also here a post about the subject

http://www.sencha.com/forum/showthread.php?248262-Reconfigure-Grid-with-response-from-server-json-object

skirtle
7 Nov 2012, 5:36 AM
Do you want to keep those strings ("cedf-c56f-18a4-4b1", etc.) or do you just want to throw them away and read only the name and state?

fujy
7 Nov 2012, 5:58 AM
Better then to return metadata in the response, and decode the response



@tvanzoelen

Thank you for your reply
But There is no metadata in the response text.
Also I don't have a grid to display the data in, I will display the some data in my customized panel, and other data in other panel on user request

fujy
7 Nov 2012, 6:00 AM
Do you want to keep those strings ("cedf-c56f-18a4-4b1", etc.) or do you just want to throw them away and read only the name and state?

@skirtle

Thank you for your reply
I don't think that I'm going to need them as they are embedded in the data, so I could use the embedded one directly

For example:



{
"users" : {
"cedf-c56f-18a4-4b1" : {
"name" : "tom",
"state" : "WA",
"id" : "cedf-c56f-18a4-4b1"
},
"ebb2-92bf-3062-7774" : {
"name" : "steve",
"state" : "NY",
"id" : "ebb2-92bf-3062-7774"
},
"fb60-b34f-6dc8-aaf7" : {
"name" : "mike",
"state" : "MA",
"id" : "fb60-b34f-6dc8-aaf7"
},
.
.
.
}
}


Sorry, I forgot to mention that

fujy
7 Nov 2012, 8:05 AM
Any Suggestions?

tvanzoelen
7 Nov 2012, 8:25 AM
Get the data with a Json Ajax Request and on callback process it



var obj = Ext.decode(response.responseText);


you can iterate the object properties with code something like



var users = obj.users;

for(var item in users){

var user = users[item];
var name = user.name;


}

skirtle
7 Nov 2012, 7:47 PM
You should be able to read that data format fine if you write a subclass of JSON reader. Take a look at getResponseData here:

http://docs.sencha.com/ext-js/4-1/source/Json2.html#Ext-data-reader-Json

Overriding readRecords should do it. Just loop through the object and rearrange the structure to something a little more straightforward. It'd be something a bit like this:


Ext.define('MyReader', {
alias: 'reader.my-reader',
extend: 'Ext.data.reader.Json',

readRecords: function(data) {
var oldUsers = data.users,
newUsers = [],
key;

for (key in oldUsers) {
newUsers.push(oldUsers[key]);
}

data.users = newUsers;

this.callParent([data]);
}
});

fujy
8 Nov 2012, 10:02 AM
@tvanzoelen, @skirtle

Thank you for your efforts

I have extended the reader.Json class and overridden the readerRecord() method, and It just works fine

I have a side question if you don't mind, concerning the nested data modeling concept itself, Why we just don't use the raw (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Model-property-raw) properties directly to access the nested data?