PDA

View Full Version : JSON model issue



TXAggie00
15 Oct 2011, 11:02 AM
Hey everyone, I have a rest json call that returns the following (in part)


{
"@id": "1318636981000",
"structure": {
"@id": "1",
"#text": "35"
},
"client": {
"@id": "1",
"#text": "178"
}
}

My question, is how do I map the values for structure and client to my model?

Thanks,
Scott

bassjobsen
16 Oct 2011, 1:50 PM
Hi,

I think you can use type: 'auto' or remapping

b.e.

fields: ['@id', {name:'structure',type: 'auto'},{name:'client',mapping: "client.#text"}]

For the last field you should escape the #, i don't know how

TXAggie00
16 Oct 2011, 2:44 PM
bassjobsen,

Thanks for the reply! I appreciate your help. I'll have to look into using "auto" for the type and how to call the fields from my tpl. I have tried mapping exactly how you have written it without much luck. I never thought of escaping the '#' character so I am guessing that is the key.

Thanks,
Scott

TXAggie00
16 Oct 2011, 7:58 PM
For those who are searching for an answer for this, this is how I did it:



fields: [
{name: 'id', mapping: '@id'},
{name: 'structure'},
{
name: 'structuretext',
convert: function(value, record) {
var structure= record.get('structure'),
structuretext= structure['#text']
return structuretext;
}
},
{
name: 'structureid',
convert: function(value, record) {
var structure= record.get('structure'),
structureid= structure['@id']
return structureid;
}
}
]