JeeShen Lee
JeeShen Lee

Reputation: 3816

Serialize Json with variable name

I got the requirement to serialize my C# class RoomType like the below

public class RoomType
{
    public string name { get; set; }
    public string url { get; set; }
    public string desc { get; set; }
}

to the below json format like this

"room_types"  :
    {
    "Fenway Room" :
        {
            "url" : "http://www.partner-site.com/hotel_commonwealth/fenway_room",
            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Fenway Park."
        },
    "Commonwealth Room" :
        {
            "url" : "http://www.partner-site.com/hotel_commonwealth/commonwealth_room",
            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue."
        }
    }

How to get the "Fenway Room" and "Commonwalth Room" to in this json format?

I tried the suggestion but still can't get how the anonymous fit into what i needed in the ActionResult. Here's my not working code now:

        var rooms = new List<HarRoomType>()
                    {
                        new HarRoomType()
                        {

                        }
                    };

        var anonymous = new
                        {
                            type = rooms.ToDictionary(x => x.name, x => new {x.currency, x.discounts})
                        };
        var response = new HotelAvailabilityResponse()
                       {
                           api_version = 4,
                           hotel_ids = new List<int>()
                                       {
                                           1,
                                           2
                                       },
                           start_date = "2014-02-21",
                           hotels = new List<HarHotel>()
                                    {
                                        new HarHotel()
                                        {
                                            hotel_id = 1,
                                            room_types = anonymous                                                    
                                        },
                                        new HarHotel()
                                        {
                                            hotel_id = 2,
                                            room_types = new List<HarRoomType>()                                                             
                                        }
                                    }
                       };
        return Json(response, JsonRequestBehavior.AllowGet);

Upvotes: 1

Views: 577

Answers (3)

user1968030
user1968030

Reputation:

Use this:

var anonymous= new {
    type= rooms.ToDictionary(
        x => x.name,
        x => new { x.url, x.desc }
    )

Upvotes: 0

aquinas
aquinas

Reputation: 23796

JavaScriptSerializer js = new JavaScriptSerializer();
List<RoomType> roomTypes = new List<RoomType>(){
    new RoomType{ desc="desc 1", name="Fenway Room", url="blah.com"},
    new RoomType{ desc="desc 2", name="Commonowealth Room", url="blah.com"},
};

If you don't care about having the name show up as a property, then:

var json = js.Serialize(roomTypes.ToDictionary(x => x.name));

If you do care about having the name show up and don't want it to:

var json2 = js.Serialize(roomTypes.ToDictionary(x => x.name, x => new { desc = x.desc, url = x.url }));

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062855

You need to shape the data as a dictionary:

RoomType[] rooms = ...

var serializeThis = new {
    room_types = rooms.ToDictionary(
        x => x.name,
        x => new { x.url, x.desc }
    )
};

Upvotes: 2

Related Questions