Silverlight 访问 ashx JSON 响应

发布于 2024-08-02 08:41:27 字数 830 浏览 3 评论 0原文

我有一个 Silverlight 应用程序,它调用与 Silverlight 控件托管在同一应用程序中的 ashx。

ashx 执行以下操作(精简):

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

现在我遇到问题的部分是如何处理 Silverlight 控件上的 ashx。

我希望调用 ashx,然后将 JSON 结果映射到我的内部 silverlight 对象中。似乎是一个非常简单的任务,但我不确定如何访问 ashx 或处理它的响应。由于 Silverlight 有 .NET 的精简版本,所以它让我感到厌烦。

有什么帮助/建议吗?

使用 Silverlight 3、ASP.NET 3.5。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-08-09 08:41:27

使用 System.Json 将字符串加载到 JsonArray 中。 JsonValue.Load() 接受响应流并可以填充 JsonArray - 从那里,您可以迭代或使用 LINQ 查询值。

链接:

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:

酒解孤独 2024-08-09 08:41:27

感谢乔恩的回复。您的链接帮助我弄清楚了这一点,我认为我应该为将来遇到这个问题的其他人提供我在这个问题中使用的代码。

处理 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);

然后,您需要使用处理 Json 的代码来实现上面指定的事件处理程序 downloader_OpenReadCompleted。在这两种情况下,下面的代码都应该包含在 using 语句中:

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

处理属于 Silverlight 框架的 Json 数据的第一种方法是添加对 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));

这两种方法最终都会为我提供一个对象列表,然后我可以根据需要使用该列表。

感谢乔恩的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文