从ASP.NET页返回JSON对象 [英] Returning JSON object from an ASP.NET page

查看:168
本文介绍了从ASP.NET页返回JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的特殊情况,我有几个解决方案,以我的问题。我想找出哪一个是比较可行的。在这种情况下,我也可以从不过我的服务器端code返回一个JSON对象,实现我的目标,我不知道它是如何做的,哪些是做的最好的方法。

In my particular situation, I have couple of solutions to my problem. I want to find out which one is more feasible. In this case, I can also achieve my goal by returning a JSON object from my server side code, however, I do not know how it is done and what is the best way of doing it.

首先,因为我只需要code返回响应我并不需要一个完整的aspx页面。所以,做我使用Web服务?处理程序?或者是有你的人做任何其他特定的方式?

First off, I don't need a full aspx page as I only need a response returned from code. So, do I use web services? handler? or is there any other specific way you people do this?

这是解决方案是否可行?做我建立使用StringBuilder类,并注入该字符串到目标aspx页面JSON字符串?是否有任何precautions?或事情,我应该知道的?

Is this solution feasible? do I build the JSON string using the StringBuilder class and inject that string into the target aspx page? are there any precautions? or things that I should be aware of?

我AP preciate你的想法。

I appreciate your ideas.

问候,

凯末尔

------------更新!------------

假设我有我的userlist.aspx页的JSON对象。然后我用它与jQuery ...

Suppose I have a JSON object in my userlist.aspx page. which then I use it with jQuery...

{"menu": {
  "id": "color1",
  "value": "color",
  "popup": {
    "menuitem": [
      {"value": "Red"},
      {"value": "Green"},
      {"value": "Yellow"}
    ]
  }
}} // example taken from the json.org/example page

现在,当我想从我的aspx页面添加一个新的菜单项,我该怎么办......我想这样我的问题是更具体的...
让我们假设我创造我的aspx code一个新的字符串,因为这样的{值:蓝}。现在我怎么注入到这一点已经存在ITEMLIST在目标页面,或者这是不是正确的?方法这种情况?如果不是还能怎么实现?
另外,如果我想要当一个新项目添加到这个列表解雇一个jQuery事件,这是怎么实现的?

now when I want to add a new menuitem from my aspx page, what do I do... I think this way my question is more specific... Lets assume I create a new string in my aspx code, as such "{"value": "Blue"}. Now how do I inject this into the already existing itemlist in the target page? or is this not the correct approach to this kind of situation? if not how else can it be achieved? Also if I wanted to fire a jquery event when a new item is added to this list, how is this achieved?

------------ 2015年8月26日更新2 ------------
当我问到这个问题的时候,我一度逼近问题的方式是另一个方面。我现在更proficcient在主题,并且可以很高兴地选择最投票的答案,因为这种方法对这个问题接受的答案显然是不应包括已经exsiting JSON和输出从code作为@DavGarcia也是一个新的提示

------------UPDATE 2 on 26 August 2015 ------------ By the time I asked this question, the way I was approaching the problem was in another aspect. I am now more proficcient in the subject and can gladly choose the most voted answer as the accepted answer since the approach to this question is clearly should not include the already exsiting JSON and output a new one from the code as @DavGarcia also suggests.

推荐答案

在你的Page_Load你将要清除出正常输出,写你自己的,例如:

In your Page_Load you will want to clear out the normal output and write your own, for example:

string json = "{\"name\":\"Joe\"}";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();

要一个C#对象转换成JSON,您可以使用一个库,如 Json.NET

To convert a C# object to JSON you can use a library such as Json.NET.

而不是让你的.aspx页面中输出JSON虽然,考虑使用Web服务(ASMX)或WCF,这两者可以输出JSON。

Instead of getting your .aspx page to output JSON though, consider using a Web Service (asmx) or WCF, both of which can output JSON.

这篇关于从ASP.NET页返回JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆