Can i use JSON.Stringify in code-behind of an ASP.Net project









up vote
8
down vote

favorite












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.



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.



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 is a function which creates and returns the JSON-string.



This is my view:



<%@ 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 = "";












share|improve this question























  • just post a snippet of what you have tried ?
    – BRAHIM Kamel
    Feb 20 '17 at 11:30










  • Possible duplicate of How can I beautify JSON for display in a TextBox?
    – David Culp
    Feb 20 '17 at 11:33










  • Okay, i edited my Explanation and added a few examples from my project.
    – Patrick Pirzer
    Feb 20 '17 at 11:38










  • what does GetContentAsJson(uuid) return?
    – Sujit.Warrier
    Feb 20 '17 at 11:39











  • No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
    – Patrick Pirzer
    Feb 20 '17 at 11:40














up vote
8
down vote

favorite












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.



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.



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 is a function which creates and returns the JSON-string.



This is my view:



<%@ 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 = "";












share|improve this question























  • just post a snippet of what you have tried ?
    – BRAHIM Kamel
    Feb 20 '17 at 11:30










  • Possible duplicate of How can I beautify JSON for display in a TextBox?
    – David Culp
    Feb 20 '17 at 11:33










  • Okay, i edited my Explanation and added a few examples from my project.
    – Patrick Pirzer
    Feb 20 '17 at 11:38










  • what does GetContentAsJson(uuid) return?
    – Sujit.Warrier
    Feb 20 '17 at 11:39











  • No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
    – Patrick Pirzer
    Feb 20 '17 at 11:40












up vote
8
down vote

favorite









up vote
8
down vote

favorite











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.



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.



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 is a function which creates and returns the JSON-string.



This is my view:



<%@ 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 = "";












share|improve this question















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.



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.



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 is a function which creates and returns the JSON-string.



This is my view:



<%@ 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 = "";









c# asp.net json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 20 '17 at 13:05









user3378165

2,23762456




2,23762456










asked Feb 20 '17 at 11:27









Patrick Pirzer

7171824




7171824











  • just post a snippet of what you have tried ?
    – BRAHIM Kamel
    Feb 20 '17 at 11:30










  • Possible duplicate of How can I beautify JSON for display in a TextBox?
    – David Culp
    Feb 20 '17 at 11:33










  • Okay, i edited my Explanation and added a few examples from my project.
    – Patrick Pirzer
    Feb 20 '17 at 11:38










  • what does GetContentAsJson(uuid) return?
    – Sujit.Warrier
    Feb 20 '17 at 11:39











  • No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
    – Patrick Pirzer
    Feb 20 '17 at 11:40
















  • just post a snippet of what you have tried ?
    – BRAHIM Kamel
    Feb 20 '17 at 11:30










  • Possible duplicate of How can I beautify JSON for display in a TextBox?
    – David Culp
    Feb 20 '17 at 11:33










  • Okay, i edited my Explanation and added a few examples from my project.
    – Patrick Pirzer
    Feb 20 '17 at 11:38










  • what does GetContentAsJson(uuid) return?
    – Sujit.Warrier
    Feb 20 '17 at 11:39











  • No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
    – Patrick Pirzer
    Feb 20 '17 at 11:40















just post a snippet of what you have tried ?
– BRAHIM Kamel
Feb 20 '17 at 11:30




just post a snippet of what you have tried ?
– BRAHIM Kamel
Feb 20 '17 at 11:30












Possible duplicate of How can I beautify JSON for display in a TextBox?
– David Culp
Feb 20 '17 at 11:33




Possible duplicate of How can I beautify JSON for display in a TextBox?
– David Culp
Feb 20 '17 at 11:33












Okay, i edited my Explanation and added a few examples from my project.
– Patrick Pirzer
Feb 20 '17 at 11:38




Okay, i edited my Explanation and added a few examples from my project.
– Patrick Pirzer
Feb 20 '17 at 11:38












what does GetContentAsJson(uuid) return?
– Sujit.Warrier
Feb 20 '17 at 11:39





what does GetContentAsJson(uuid) return?
– Sujit.Warrier
Feb 20 '17 at 11:39













No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
– Patrick Pirzer
Feb 20 '17 at 11:40




No, it's just a string variable for getting data. That data is used for creating the content of the JSON.
– Patrick Pirzer
Feb 20 '17 at 11:40












2 Answers
2






active

oldest

votes

















up vote
16
down vote



accepted










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);


Edit: JSON.stringify() is a client side(browser) functionality. So you can't do that on the server side.






share|improve this answer





























    up vote
    0
    down vote













    You can use something like



    JsonConvert.SerializeObject(ob)


    From library: Newtonsoft.Json






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42343342%2fcan-i-use-json-stringify-in-code-behind-of-an-asp-net-project%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      16
      down vote



      accepted










      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);


      Edit: JSON.stringify() is a client side(browser) functionality. So you can't do that on the server side.






      share|improve this answer


























        up vote
        16
        down vote



        accepted










        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);


        Edit: JSON.stringify() is a client side(browser) functionality. So you can't do that on the server side.






        share|improve this answer
























          up vote
          16
          down vote



          accepted







          up vote
          16
          down vote



          accepted






          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);


          Edit: JSON.stringify() is a client side(browser) functionality. So you can't do that on the server side.






          share|improve this answer














          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);


          Edit: JSON.stringify() is a client side(browser) functionality. So you can't do that on the server side.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 20 '17 at 11:41









          radu florescu

          3,25484983




          3,25484983










          answered Feb 20 '17 at 11:33









          user3378165

          2,23762456




          2,23762456






















              up vote
              0
              down vote













              You can use something like



              JsonConvert.SerializeObject(ob)


              From library: Newtonsoft.Json






              share|improve this answer
























                up vote
                0
                down vote













                You can use something like



                JsonConvert.SerializeObject(ob)


                From library: Newtonsoft.Json






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can use something like



                  JsonConvert.SerializeObject(ob)


                  From library: Newtonsoft.Json






                  share|improve this answer












                  You can use something like



                  JsonConvert.SerializeObject(ob)


                  From library: Newtonsoft.Json







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 20:18









                  DaniCode

                  19028




                  19028



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42343342%2fcan-i-use-json-stringify-in-code-behind-of-an-asp-net-project%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党