Doggo
Doggo

Reputation: 2321

ASP.NET json.Net xml to json

I tried to convert some xml Code in my Webpage. So I tried to using json.net. All worked well until I tried to display my string hello. My aspx.cs Site looks like this:

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 = "[email protected]",
            DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc),
        };
        string hello = JsonConvert.SerializeObject(account, Formatting.Indented);
        }
}

And my aspx Site looks like this.

<%@ 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>

What can I do that I can use the string "hello".

Thanks and have a good day :D

Upvotes: 1

Views: 467

Answers (1)

Vadim Iarovikov
Vadim Iarovikov

Reputation: 1961

You don't have access to hello variable. You can solve it by creating property Hello

Something like this

    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 = "[email protected]",
            DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc),
        };
        this.Hello = JsonConvert.SerializeObject(account, Formatting.Indented);
    }

And on your page, you can call it

<div><% = this.Hello %></div>

Upvotes: 3

Related Questions