C#list to Serialize json string

时间:2014-06-23 06:36:30

标签: c# mysql json

我试图将我的mysql表数据转换为json字符串。在为Serialize创建对象时,我无法将List值作为对象

**示例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; }
        }

我的SQL表 -

enter image description here

序列化功能 -

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

编辑1 -

我获得了可用性,房间和根对象。所以我有三个json字符串。现在我想将三个jsons包含为一个json字符串。

对象1 - ro 对象2 - h 对象3 - r

  • {&#34; date&#34;:&#34; 2014-06-07&#34;,&#34; free&#34;:1}
  • {&#34; roomId&#34;:&#34; 3&#34;,&#34; availability&#34;:null}
  • {&#34; ACCESSKEY&#34;:&#34; 7eb228097576abf56968e9845ab51b90&#34;&#34;的channelID&#34;:&#34; 103&#34;&#34;酒店&#34;:[ {&#34; hotelId&#34;:&#34; 1&#34;&#34;房间&#34;:空}]}

现在我想合并这些jsons字符串。我怎么能这样做?

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

1 个答案:

答案 0 :(得分:0)

您可以使用NewtonSoft.Json序列化

示例:

var jsonString = JsonConvert.SerializeObject(ro);

返回:

{"accessKey":"7eb228097576abf56968e9845ab51b90","channelId":"103","hotels":[{"hotelId":"2","rooms":[{"roomId":"1","availability":[{"date":"2014-06-06","free":1}]}]}]}