Hybrid View
-
7 Nov 2012 3:16 AM #1
Answered: Random Field names in JSON Data
Answered: Random Field names in JSON Data
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:
I know that the model of that data will be something like thisCode:{ "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" }, . . . ] }
But what if the JSON response data are like theseCode:Ext.define('Foo.model.User',{ extend: 'Ext.data.Model', fields: [ {name: 'name' ,type: 'string'}, {name: 'state' ,type: 'string'}, {name: 'id' ,type: 'string'}, ], });
Note: I don't have any control on the response
And of course the "cedf-c56f-18a4-4b1", "ebb2-92bf-3062-7774" ... etc, are totally unknown to meCode:{ "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" }, . . . } }
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
-
Best Answer Posted by skirtle
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/so...ta-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:
Code: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]); } });
-
7 Nov 2012 4:46 AM #2Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,017
- Vote Rating
- 23
- Answers
- 75
Better then to return metadata in the response, and decode the response
From the MetaData create a new Store and reconfigure the gridCode:var obj = Ext.decode(r.responseText);
Also here a post about the subjectCode:grid.reconfigure(newStore)
http://www.sencha.com/forum/showthre...er-json-object
-
7 Nov 2012 5:36 AM #3
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?
-
7 Nov 2012 6:00 AM #4
@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:
Sorry, I forgot to mention thatCode:{ "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" }, . . . } }
-
7 Nov 2012 8:05 AM #5
-
7 Nov 2012 8:25 AM #6Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,017
- Vote Rating
- 23
- Answers
- 75
Get the data with a Json Ajax Request and on callback process it
you can iterate the object properties with code something likeCode:var obj = Ext.decode(response.responseText);
Code:var users = obj.users; for(var item in users){ var user = users[item]; var name = user.name; }
-
7 Nov 2012 5:58 AM #7


Reply With Quote
