Silverlight accesses ashx JSON response

I have a Silverlight application that is being called in ashx hosted in the same application as the Silverlight control.

In ashx, the following is executed (removed):

// 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 I am having problems with what to do with ashx on my Silverlight control.

I want to call ashx and then map the JSON result to my inner silverlight objects. This seems to be a pretty simple task, but I don't know how to access ashx or get an answer from it. Since Silverlight has a stripped down version of .NET, it throws me off.

Any help / suggestions?

Using Silverlight 3, ASP.NET 3.5.

+3
source share
2

System.Json JsonArray. JsonValue.Load() JsonArray - , LINQ .

:

+1

. , , , , .

Json. Json.

// 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);

downloader_OpenReadCompleted, , Json. using:

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

Json, Silverlight, 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);
}

, Silverlight , :

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

, .

Jon!

0

Source: https://habr.com/ru/post/1715210/


All Articles