title: How to serialize a class to a JSON string in C# to use in the Properties App dictionary in Xamarin.forms
body: <p>I am trying to save the state of some labels on an Xamarin.Forms App. I am following the steps of <a href="https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/" rel="nofollow noreferrer">Creating Mobile Apps with Xamarin.Forms</a> book, it shows how to store value per value but recommends this for when you have several info:</p>
  
  <blockquote>
    <p>An application with many more items might want to consolidate them in a class named AppSettings (for example), serialize that class to an XML or a JSON string, and then save the string in the dictionary.</p>
  </blockquote>
  
  <p>I am lost about how to do it, I created a simple class</p>
  
  <pre><code>class AppSettings
  {
      public double? gain = null;
      public string mainTitle = "my title";
  }
  </code></pre>
  
  <p>In my constructor I recover the properties in this way</p>
  
  <pre><code>      //! Recover appSettings if some
  
          IDictionary&lt;string, object&gt; properties = Application.Current.Properties;
          if (properties.ContainsKey("mainTitle"))
          {
              mainTitle.Text = properties["mainTitle"] as string;
          }
  
          //! New code for loading previous keypad text.
          App app = Application.Current as App;
          mainTitle.Text = app.MainTitle;
  </code></pre>
  
  <p>And the event that I use to save it is:</p>
  
  <pre><code>void onHomeOptionButtonClicked(object sender, EventArgs args)
          {
  
              Button buttonClicked = (Button)sender;
  
              mainTitle.Text =  buttonClicked.Text;
              // !Save keypad text.
              App app = Application.Current as App;
              app.MainTitle = mainTitle.Text;
          }
  </code></pre>
  
  <p>What I do not know is how to structure my Class so it can be serialized to JSON (<strong>I also do not know how to do this serialization</strong>)</p>
  
  <p>I wanted to create a class in C# with this Javascript's idea:</p>
  
  <pre><code>myAppSettings = {
      HomePage : {
          name1 : "value1",
          name2 : "value2"
          name3 : {
              name3.name1 : "value3.1"
          }
      },
      OtherPage:{
          name1 : "value1",
          name2 : "value2"
      },
      .
      .
      .
  };
  </code></pre>
  
  <p>Which is the correct way to do it in C#?</p>