views:

820

answers:

2

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.

+1  A: 

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:

Jon Galloway
A: 

Thanks for the reply Jon. Your links helped me figure it out and I thought I should include the code I used in this question for others that come across this question in the future.

Two ways of handling the Json. For both methods you need to setup a handler to get the Json data.

// This gets the URL to call to get the Json data
Uri uri = GetSomeUrl();
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(uri);

You then need to implement the event handler downloader_OpenReadCompleted specified above with the code to handle the Json. In both case the code below should be wrapped in a using statement:

using (System.IO.Stream strResult = e.Result)
{
}

First way to handle the Json data that is part of the Silverlight framework is to add a reference to System.Json.

JsonArray jsonArray = (JsonArray)JsonArray.Load(e.Result);
List<SomeObject> lst = new List<SomeObject>();
foreach (System.Json.JsonObject obj in jsonArray)
{
    SomeObject obj = new SomeObject();
    obj.ID = int.Parse(obj["ID"].ToString();
    obj.Description = obj["Description"].ToString();
    obj.Value = double.Parse(obj["Value"].ToString());
    lst.Add(obj);
}

The other way that is possible with or without Silverlight is:

System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
    new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<SomeObject>));
List<SomeObject> lst = (List<SomeObject>)(serializer.ReadObject(strResult));

Both methods end up getting me a list of my objects which I can then use as I see fit.

Thanks for the help Jon!

Kelsey