1

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

3 Answers 3

2

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 }
    )
};
Sign up to request clarification or add additional context in comments.

4 Comments

How can this works on ASP MVC? I updated a code i need on ASP MVC
@JeeShenLee that should work fine in MVC. If you are seeing a specific problem, you'll need to tell me what it says
updated the question. Getting object incompatible problem at room_types = anonymous
@JeeShenLee can you post the exact, verbatim message? also indicating whether it is the compiler versus the runtime that raises the error? and if the latter: the .StackTrace?
0
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 }));

Comments

0

Use this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.