PDA

View Full Version : Nested JSON loaded into FormPanel



perkinsit
13 Oct 2009, 11:12 PM
I am trying to load a nested JSON object into a FormPanel. I can get it working if I use a flat JSON object, so I know all of the communication to and from the server is working correctly, as well as the JsonReader for the FormPanel. Basically, here is the JSON I want to use:


{
"success": "true",
"cons_record": [
{
"consumer_main": {
"cons_id": "10",
"client_id": "0",
"cons_fname": "Mandy",
"cons_lname": "Mason",
"cons_ss": null,
"cons_dob": null,
"cons_gender": null,
"cons_medicaid": null,
"cons_mod": "2009-09-24 13:43:30",
"cons_crea": "0000-00-00 00:00:00"
}
}
]
}Here is my reader ( of course this is the reader definition within the FormPanel):


reader: new Ext.data.JsonReader({
root: 'cons_record',
successProperty: "success",
fields: [
//consumer_main
{name: 'consumer_main[cons_id]', type: 'string', mapping: 'consumer_main.cons_id'},
{name: 'consumer_main[cons_fname]', type: 'string', mapping: 'consumer_main.cons_fname'},
{name: 'consumer_main[cons_lname]', type: 'string', mapping: 'consumer_main.cons_lname'},
{name: 'consumer_main[cons_ss]', type: 'string', mapping: 'consumer_main.cons_ss'},
{name: 'consumer_main[cons_dob]', type: 'string', mapping: 'consumer_main.cons_dob'},
{name: 'consumer_main[cons_gender]', type: 'string', mapping: 'consumer_main.cons_gender'}

]
})As I said, when I use a flat file, and adjust the mapping accordingly, I can load the form. With the nested Json, I am not able to access the data when it is loaded by the reader. I plan to add additional objects for items such as address, etc, so this is really just a proof of concept to get it working. Thanks for any help on this. I have been stumped for hours.

John Sourcer
13 Oct 2009, 11:50 PM
Try changing the root element to 'data' and then call form.load(url:..., method:..., params:...)

perkinsit
14 Oct 2009, 12:44 AM
Thanks for your response. It seems to be working the way I posted. Now I have an additional problem. When I add my Address object to the existing Json object, the loading of the data fails and I get a message in the console stating that "consumer_main is not defined". Is there an example or documentation stating how I should be formatting this string? Here is what I have so far for my Json object:


{
"success": "true",
"data": [
{
"consumer_main": {
"cons_id": "7",
"client_id": "0",
"cons_fname": "Jesss",
"cons_lname": "James",
"cons_ss": null,
"cons_dob": null,
"cons_gender": null,
"cons_medicaid": null,
"cons_mod": "2009-09-24 04:30:15",
"cons_crea": "0000-00-00 00:00:00"
}
},
{
"consumer_address": {
"cons_addr_id": "1",
"cons_id": "9",
"client_id": "1",
"cons_name": null,
"cons_addr1": null,
"cons_addr2": null,
"cons_city": null,
"cons_state": null,
"cons_zip": null,
"cons_phone": null,
"cons_email": null,
"cons_mod": "2009-10-14 04:23:07",
"cons_crea": "0000-00-00 00:00:00"
}
}
]
}I already checked to make sure the null values were not causing the issue. It seems odd that the consumer_main is the one throwing the error because that was working before I added the consumer_address object. I also checked this code in JSONLint and it validates.
Again, thanks for the help!

perkinsit
17 Oct 2009, 11:50 PM
Does anyone need more info on this? I am really at a stand still until I can resolve this. Thanks!

Animal
18 Oct 2009, 2:57 AM
You've changed your JSON haven't you?

So have you changed how your JsonReader is configured? What is "root" supposed to be?

And your new JSON will result in TWO records (though both are different, so you can't really capture a sequence of records). And to load a form, you need a single record.

Why not



{
"success": "true",
"cons_record": [{
"consumer_main": {
"cons_id": "7",
"client_id": "0",
"cons_fname": "Jesss",
"cons_lname": "James",
"cons_ss": null,
"cons_dob": null,
"cons_gender": null,
"cons_medicaid": null,
"cons_mod": "2009-09-24 04:30:15",
"cons_crea": "0000-00-00 00:00:00"
},
"consumer_address": {
"cons_addr_id": "1",
"cons_id": "9",
"client_id": "1",
"cons_name": null,
"cons_addr1": null,
"cons_addr2": null,
"cons_city": null,
"cons_state": null,
"cons_zip": null,
"cons_phone": null,
"cons_email": null,
"cons_mod": "2009-10-14 04:23:07",
"cons_crea": "0000-00-00 00:00:00"
}
}]
}


?