我试图在我的网页中转换一些 xml 代码。所以我尝试使用 json.net。 在我尝试显示我的字符串 hello 之前,一切都很好。我的 aspx.cs 站点如下所示:
aspx.cs
public partial class json : System.Web.UI.Page
{
public class Account
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime DOB { get; set; }
}
public void Page_Load(object sender, EventArgs e)
{
Account account = new Account
{
Name = "John Doe",
Email = "john@microsoft.com",
DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc),
};
string hello = JsonConvert.SerializeObject(account, Formatting.Indented);
}
}
我的 aspx 网站看起来像这样。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="json.aspx.cs" Inherits="json" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><% = this.hello %></div>
</form>
</body>
</html>
我该怎么做才能使用字符串“hello”。
谢谢,祝你有美好的一天 :D
最佳答案
您无权访问 hello 变量。 您可以通过创建属性 Hello
来解决它像这样
public class Account
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime DOB { get; set; }
}
public string Hello { get; set; }
public void Page_Load(object sender, EventArgs e)
{
Account account = new Account
{
Name = "John Doe",
Email = "john@microsoft.com",
DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc),
};
this.Hello = JsonConvert.SerializeObject(account, Formatting.Indented);
}
在你的页面上,你可以调用它
<div><% = this.Hello %></div>
关于c# - ASP.NET json.Net xml转json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660165/