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
** Sample Json String -**
{"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"2","rooms":[{"roomId":"1","availability":[{"date":"2014-06-06","free":1}]}]}]}
C# Classes-
public class Availability
{
public string date { get; set; }
public int free { get; set; }
}
public class Room
{
public string roomId { get; set; }
public List<Availability> availability { get; set; }
}
public class Hotel
{
public string hotelId { get; set; }
public List<Room> rooms { get; set; }
}
public class RootObject
{
public string accessKey { get; set; }
public string channelId { get; set; }
public List<Hotel> hotels { get; set; }
}
My SQL Table -
Serialize Function -
public string cc()
{
string s = "";
RootObject ro = new RootObject();
ro.accessKey = "7eb228097576abf56968e9845ab51b90";
ro.channelId = "103";
ro.hotels = new List<Hotel>();
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;
}
EDIT 1 -
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.
object 1 - ro Object 2 - h Object 3 - r
- {"date":"2014-06-07","free":1}
- {"roomId":"3","availability":null}
- {"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"1","rooms":null}]}
Now I want to merge these jsons string. How can i do this?
public string cc()
{
string s = "";
RootObject ro = new RootObject(); // First Object
ro.accessKey = "7eb228097576abf56968e9845ab51b90";
ro.channelId = "103";
ro.hotels = new List<Hotel>();
//List<Hotel> hotel = new List<Hotel>();
//List<Room> r = new List<Room>();
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;
}
}