views:

468

answers:

2

I'm trying to bind a JSON array to a datagrid in Silverlight 3. I do not get any exceptions but I do not see the column values in the datagrid. I do see the rows though, but I do not know what the binding property should be. I do not want to create a class, populate the class and the bind. That works. I do not know what columns and datatypes the json string contains. I want the datagrid to just show all columns that are present in the json object.

Following is the code

Dim J As JsonArray = JsonArray.Load(New StringReader("[{'name':'arun', 'age':26, good:true},{'name':'kumar', 'age':28, good:false}]"))

For Each JJ In J
    MessageBox.Show(JJ("name")) 'This shows the proper names'
Next

Dim c = New DataGridTextColumn()
c.Binding = New Binding("name")
GridUsers.Columns.Add(c)
GridUsers.ItemsSource = J

I do see 2 rows in the grid, but the columns values are always blank. What am I missing the binding property?

Many Thanks, Arun

A: 

Have you tried JSON.NET? Version 3 apparently has support specifically for Silverlight 3:

http://james.newtonking.com/archive/2008/08/25/json-net-3-0-released.aspx

Dave Swersky
Thanks Dave, I have heard of JSON.net, but my problem is not with parsing the JSON. That works fine with System.JSON. My problem is with binding an anonymous object with a datagrid
Arun
A: 

This looks similar to the problem of binding to dynamically created columns. See http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/][1]. The secret is to make the entire object the binding source and use a value converter and a converter parameter to identify individual members.

mikemay