Search code examples
c#asp.netsilverlightjsonashx

Silverlight access an ashx JSON response


I have a Silverlight application that is calling out to an ashx that is hosted in the same application as the Silverlight control.

The ashx does the following (stripped down):

// Basic object
class SomeObject
{
    int ID { get; set; }
    string Description { get; set; }
    double Value { get; set; }
}


// ASHX details
DataLayer dl = GetDataLayer();
List<SomeObject> lst = dl.ListObjects();
string result = "";
if (lst != null)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    result = serializer.Serialize(lst);
}
context.Response.ContentType = "application/json";
context.Response.Write(result);
context.Response.End();

Now the part I am having trouble with is what to do with the ashx on my Silverlight control.

I am looking to call the ashx and then map the JSON result into my internal silverlight objects. Seems like a pretty simple task but I am not sure how to access the ashx or deal with the response from it. Since Silverlight has a stripped down version of .NET it is throwing me for off.

Any help / suggestions?

Using Silverlight 3, ASP.NET 3.5.


Solution

  • Use System.Json to load the string into a JsonArray. JsonValue.Load() takes a response stream and can populate a JsonArray - from there, you can either iterate through or use LINQ to query the values.

    Links: