PDA

View Full Version : Complex Json data With dynamic objects/fields from Solr Search Engine.



rohangpatil
24 Jun 2010, 9:17 PM
Hi,

I have stumbled upon a profound problem. I have a JSON returned from Solr Search server.

it looks something like this.

{
"response":
{


"docs":[




{



"id":"DOC1",
"text":"blah blah"
},



{
"id":"DOC2",
"text":"blah blah"
}



]

},
"morelikethis":{
"DOC1":{
"docs":[




{
"id":"MLTDOC3",
"text":"Blah Blah"
},
{
"id":"MLTDOC4",
"text":"Blah Blah"
}



]


},
"DOC2":{

"docs":[

{
"id":"MLTDOC1",
"text":"Blah Blah"
},
{
"id":"MLTDOC2",
"text":"Blah Blah"
}

]

}
}
}As you can see, the IDs of the docs in "response" object have become, objects itself under "morelikethis" object (Hi-lighted in red).

I how to access the objects under morelikethis object ??

all i can think of is store the ids in an array, then put the whole json reader code inside a loop..will this help?

Any other ideas ??

evant
24 Jun 2010, 9:36 PM
Probably easier to write a custom reader. It's pretty trivial to do.

rohangpatil
25 Jun 2010, 12:32 AM
hmm..If its trivial enough, then how to go about it ?

Should i extend one of the classes already present ?

Animal
25 Jun 2010, 1:10 AM
Extract the response data into a more regular Array format in a root extraction function



Document = Ext.data.Record.create([ 'id', 'text' ]);
new Ext.data.JsonReader({
root: function(rawData) {
// massage the "morelikethis" object into an Array which JsonReader can deal with.
},
idProperty: 'id'
}, Document);

rohangpatil
25 Jun 2010, 2:35 AM
Extract the response data into a more regular Array format in a root extraction function



Document = Ext.data.Record.create([ 'id', 'text' ]);
new Ext.data.JsonReader({
root: function(rawData) {
// massage the "morelikethis" object into an Array which JsonReader can deal with.
},
idProperty: 'id'
}, Document);



Or else can i,
1)write the above code in a function which, accepts the id(For the path, we can append 'morelikethis.'+ID+'.docs'), and returns data records.
2)store the IDs in an array, using the readers which i am currently using, and loop through the array, pass ID to function and get the data records and append this collection into a unified collection ?

Condor
25 Jun 2010, 3:31 AM
You could use:

var store = new Ext.data.JsonStore({
root: 'response.docs',
idProperty: 'id',
fields: [
'id',
'text',
{name: 'docs', mapping: 'id'}
]
});
store.fields.get('docs').convert = function(id){
return store.reader.jsonData.morelikethis[id].docs;
};