我可以在ASP.Net项目的代码隐藏中使用JSON.Stringify吗? [英] Can I use JSON.Stringify in code-behind of an ASP.Net project?
问题描述
在ASP.NET项目(MVP模式)的代码隐藏中,我在一个演示者中得到一个字符串,其中包含类似于JSON文件内容的内容.
In the code-behind of an ASP.NET project (MVP-pattern) I get in one of the presenters a string which contains something which looks like the content of a JSON file.
然后,使用该字符串设置视图的属性之一(分配给演示者的视图).
Then I set one of the properties of the view - which is assigned to the presenter - with that string.
在视图中,该字符串显示在TextBox中,但是它看起来并不好,因为它没有使用换行符和换行符进行结构化. 我知道有一个叫做Stringify的JSON函数,可以使这样的字符串变得漂亮.
In the view the string is displayed in a TextBox, but it doesn't look good, because it is not structured with newlines and line feeds. I know there is a JSON-function called Stringify which can make such strings pretty.
我可以在代码背后调用该JSON函数吗? 例如,当我在演示者中设置视图的属性时?
Can I call that JSON-function in code-behind? Per example when I set the property of the view in the presenter?
所以我在演示者中设置了它:
So I set it in the presenter:
this.view.ContentAsJson = GetContentAsJson(uuid);
这是我想做的,如果可能的话:
This is what I would like to do, if it's possible:
this.view.ContentAsJson = JSON.Stringify(GetContentAsJson(uuid));
GetContentAsJson
是创建并返回JSON字符串的函数.
GetContentAsJson
is a function which creates and returns the JSON-string.
这是我的观点:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ContentJsonView.ascx.cs" Inherits="WebCenter.PP.PI.WebGui.View.FolderView.ContentJsonView" %>
<%@ Import Namespace="WebCenter.PP.Common.Domain" %>
<div id="DivContentJson" class="clearfix">
<p>
<asp:TextBox runat="server" ID="TbContentJson" TextMode="MultiLine" Height="100%" Width="100%" />
</p>
</div>
这是视图中的属性,该属性获取字符串:
This is the property in the view which gets the string:
public string ContentAsJson
{
set
{
if (!string.IsNullOrEmpty(value))
{
TbContentJson.Text = value;
}
else
{
TbContentJson.Text = "";
}
}
}
推荐答案
JSON.stringify()
实际上将JavaScript对象转换为字符串,您可以在服务器端像这样进行操作:
JSON.stringify()
Actually converts a JavaScript object into a string, you can do it in server side like this:
using System.Web.Script.Serialization;
var json = new JavaScriptSerializer().Serialize(obj);
JSON.stringify()
是客户端(浏览器)功能.因此,您不能在服务器端执行此操作.
JSON.stringify()
is a client side(browser) functionality. So you can't do that on the server side.
这篇关于我可以在ASP.Net项目的代码隐藏中使用JSON.Stringify吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!