JSON New Line Issue Resolved

12:23

JsonParse.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsonParse.aspx.cs" Inherits="MVC_Application.JsonParse" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        body {
            background-color: #efefef;
        }
        fieldset {
            margin-left: 21%;
            margin-top: 8%;
            margin-right: 32%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <fieldset>
            <legend>JSON New Line Issue Resolved</legend>

                First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            <br />
                Last Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            <br />
                Description: <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Width="500px" Height="100px"></asp:TextBox>
            <br />
            <br />
                <asp:Button ID="txtSubmit" runat="server" Text="Submit" OnClick="txtSubmit_Click" />
            <br />
            <br />
                <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
        </fieldset>
    </form>
</body>
</html>

JsonParse.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MVC_Application
{
    public partial class JsonParse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void txtSubmit_Click(object sender, EventArgs e)
        {
            string t1 = "FirstName";
            string t2 = "LastName";
            string t3 = "Description";

            string v1 = txtFirstName.Text.Trim();
            string v2 = txtLastName.Text.Trim();
            string v3 = txtDescription.Text.Trim();

            var jsonWithParse = "{";
            jsonWithParse += "\"" + t1 + "\"" + ":" + "\"" + parseJSON(v1) + "\",";
            jsonWithParse += "\"" + t2 + "\"" + ":" + "\"" + parseJSON(v2) + "\",";
            jsonWithParse += "\"" + t3 + "\"" + ":" + "\"" + parseJSON(v3) + "\"";
            jsonWithParse += "}";

            lblMsg.Text = "Complete JSON :" + jsonWithParse;
        }

        public static string parseJSON(string uglyJson)
        {
            var sb = new StringBuilder(uglyJson);
            sb.Replace("\"", """)
                .Replace("\r\n", "<br/>");
            return sb.ToString();
        }
    }
}

You Might Also Like

0 comments