title: C# list to Serialize json string
body: <p>I have tried to convert my mysql table data to json string. while creating object for Serialize, I can't get the List values as object</p>
  
  <p>** Sample Json String -**</p>
  
  <pre><code>{"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"2","rooms":[{"roomId":"1","availability":[{"date":"2014-06-06","free":1}]}]}]}
  </code></pre>
  
  <p><strong>C# Classes-</strong> </p>
  
  <pre><code>public class Availability
          {
              public string date { get; set; }
              public int free { get; set; }
          }
  
          public class Room
          {
              public string roomId { get; set; }
              public List&lt;Availability&gt; availability { get; set; }
          }
  
          public class Hotel
          {
              public string hotelId { get; set; }
              public List&lt;Room&gt; rooms { get; set; }
          }
  
          public class RootObject
          {
              public string accessKey { get; set; }
              public string channelId { get; set; }
              public List&lt;Hotel&gt; hotels { get; set; }
          }
  </code></pre>
  
  <p><strong>My SQL Table -</strong></p>
  
  <p><img src="https://i.stack.imgur.com/zpTxE.jpg" alt="enter image description here"></p>
  
  <p><strong>Serialize Function -</strong></p>
  
  <pre><code>public string cc()
  {
  
  string s = "";
  RootObject ro = new RootObject();
  ro.accessKey = "7eb228097576abf56968e9845ab51b90";
  ro.channelId = "103";
  ro.hotels = new List&lt;Hotel&gt;();   
  ro.hotels. // Here I want to access the room list. But I can't get
  
  
  Hotel h = new Hotel();
  
  string config = "server=localhost;username=mcubic;password=mcs@2011$;database=test";
  
  MySqlConnection connection = new MySqlConnection(config);
  
  string query = "select * from test1";
  
  MySqlCommand command = new MySqlCommand(query, connection);
  connection.Open();
  MySqlDataReader Reader = command.ExecuteReader();
  while (Reader.Read())
  {            
      h.hotelId = Reader[1].ToString();
  }
  connection.Close();
  
  ro.hotels.Add(h);
  JavaScriptSerializer js = new JavaScriptSerializer();
  s = js.Serialize(ro);
  
  return s;
  }
  </code></pre>
  
  <p><strong>EDIT 1 -</strong></p>
  
  <p>I got availability, Room and root object. so i got three json strings. now i want to include the three jsons as one json string.</p>
  
  <p>object 1 - ro 
  Object 2 - h 
  Object 3 - r</p>
  
  <ul>
  <li>{"date":"2014-06-07","free":1}    </li>
  <li>{"roomId":"3","availability":null}  </li>
  <li>{"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"1","rooms":null}]}</li>
  </ul>
  
  <p>Now I want to merge these jsons string. How can i do this?</p>
  
  <pre><code>    public string cc()
      {
  
      string s = "";
      RootObject ro = new RootObject(); // First Object 
      ro.accessKey = "7eb228097576abf56968e9845ab51b90";
      ro.channelId = "103";
      ro.hotels = new List&lt;Hotel&gt;();        
  
      //List&lt;Hotel&gt; hotel = new List&lt;Hotel&gt;();
      //List&lt;Room&gt; r = new List&lt;Room&gt;();
  
      Hotel h = new Hotel(); // 2nd Object
      Room r = new Room(); // 3rd object
      Availability a = new Availability();
  
      string config = "server=localhost;username=mcubic;password=mcs@2011$;database=test";
  
      MySqlConnection connection = new MySqlConnection(config);
  
      string query = "select * from test1";
  
      MySqlCommand command = new MySqlCommand(query, connection);
      connection.Open();
      MySqlDataReader Reader = command.ExecuteReader();
      while (Reader.Read())
      {
          a.date = Reader[3].ToString();
          a.free = Convert.ToInt32(Reader[4].ToString());
      }
      connection.Close();
  
      query = "select * from test1";
  
      command = new MySqlCommand(query, connection);
      connection.Open();
      Reader = command.ExecuteReader();
      while (Reader.Read())
      {
          r.roomId = Reader[2].ToString();
      }
      connection.Close();
  
      query = "select * from test1";
  
      command = new MySqlCommand(query, connection);
      connection.Open();
      Reader = command.ExecuteReader();
      while (Reader.Read())
      {
          h.hotelId = Reader[1].ToString();
      }
      connection.Close();
  
      r.availability.Add(a); // Object reference not set to an instance of an object Error
      //h.rooms.Add(r);
  
      ro.hotels.Add(h);
  
      JavaScriptSerializer js = new JavaScriptSerializer();
      s = js.Serialize(a); // Output - {"date":"2014-06-07","free":1}
      s = js.Serialize(r); // Output -  {"roomId":"3","availability":null}
      s = js.Serialize(ro); // Output - {"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"1","rooms":null}]}
  
      return s;
      }
  }
  </code></pre>