Wednesday, March 31, 2010

GWT-EXT Read nested JSON object

Here is my JSON.

{
 "pk": 9990,
"title": "World Event",
"description": "Description of World Event",
"idea": {
     "ideafk": "9840",
      "ideastring": "World"
           },
 "streetAddress": "World Event's street ad"
 }

You can see it is has nested attributes. The attribute idea has sub-attributes ideafk and ideastring. I wanted to read the value of ideastring in a column and wanted to keep the value of ideafk in the recordDef for further references. Following is the solution.

final RecordDef recordDef = new RecordDef(new FieldDef[] {
new IntegerFieldDef("pk"),
new StringFieldDef("idea", "idea.ideaString"),  // here you name ideaString as idea in recordDef.
new StringFieldDef("title"),
new StringFieldDef("ideapk", "idea.ideapk"),

ColumnConfig ideaStr = new ColumnConfig("Idea", "idea", 45, true);  // In recordDef ideaString was called idea.
ColumnConfig eventTitle = new ColumnConfig("Title", "title", 45, true);