使用VB在ASP.NET中序列化JSON

时间:2013-10-22 06:37:58

标签: asp.net json vb.net

我在ASP.NET中使用VB,我一直在尝试从WebService(webmethod)序列化以下JSON。 plz帮助我使用的确切属性。

{
    "api_version" : 4 ,
    "hotel_ids" : [97497],
    "start_date" : "2013-07-01",
    "end_date" : "2013-07-03",
    "num_adults" : 2,
    "num_rooms" : 1,
    "currency" : "USD",
    "user_country" : "US",
    "device_type" : "d",
    "query_key" : "6167a22d1f87d2028bf60a8e5e27afa7_191_1360299600000_2_2",
    "lang" : "en_US",
    "num_hotels" : 1,
    "hotels" :
        [
            {
                "hotel_id": 97497,
                "room_types":
                    {
                        "Fenway Room":
                            {
                                "url": "http: //www.partner-site.com/hotel_commonwealth/fenway_room?start_date=2013-07-01&end_date=2013-07-05&num_adults=2",
                                "price": 178.50,
                                "fees": 80,
                                "fees_at_checkout": 0,
                                "taxes": 20,
                                "taxes_at_checkout": 0,
                                "final_price": 278.50,
                                "discounts":
                                    [
                                        {
                                            "marketing_text": "10% off entire stay during July",
                                            "is_percent": true,
                                            "amount": 10,
                                            "price": 20,
                                            "fees": 0,
                                            "fees_at_checkout": 0,
                                            "taxes": 0,
                                            "taxes_at_checkout": 0,
                                            "final_price": 20
                                        },
                                        {
                                            "marketing_text": "1% off web special",
                                            "is_percent": true,
                                            "amount": 1,
                                            "price": 2,
                                            "fees": 0,
                                            "fees_at_checkout": 0,
                                            "taxes": 0,
                                            "taxes_at_checkout": 0,
                                            "final_price": 2
                                        },
                                        {
                                            "marketing_text": "Waive property fee",
                                            "is_percent": false,
                                            "amount": 25,
                                            "price": 0,
                                            "fees": 25,
                                            "fees_at_checkout": 0,
                                            "taxes": 0,
                                            "taxes_at_checkout": 0,
                                            "final_price": 25
                                        }
                                    ],
                                "currency": "USD",
                                "num_rooms": 1,
                                "room_code": "SINGLE",
                                "room_amenities":
                                    [
                                        "BREAKFAST_AND_LUNCH_INCLUDED",
                                        "ROOM_WITH_A_VIEW"
                                    ]
                            }
                    }
            }
        ]
}

1 个答案:

答案 0 :(得分:0)

使用这样的代码: 这是我的实体类

Public Class Book

   ' autocomplete example needs "id", "value" and the "label" variables to be sent back.
   ' do not change or remove "id", "value" and the "label" variables
   Public Property id() As String
   Public Property label() As String
   Public Property value() As String

   Public Property Author() As String
   Public Property Genre() As String
   Public Property Price() As String
   Public Property Publish_date() As String
   Public Property Description() As String
End Class


Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    '  Query string 'term' is for autocomplete. By default, it sends the variable 
    '  "term" with the search word to the backend page.
    Dim searchText As String = context.Request.QueryString("term")
    Dim books As Collection = New Collection

    Dim ds As New DataSet()
    ds.ReadXml(HttpContext.Current.Server.MapPath("jsonfile"))
    Dim dv As DataView = ds.Tables(0).DefaultView
    dv.RowFilter = [String].Format("title like '{0}*'", searchText.Replace("'", "''"))

    Dim book As Book
    For Each myDataRow As DataRowView In dv
        book = New Book()
        book.id = myDataRow("id").ToString()
        book.value = myDataRow("title").ToString()
        book.label = myDataRow("title").ToString()
        book.Author = myDataRow("author").ToString()
        book.Genre = myDataRow("genre").ToString()
        book.Price = myDataRow("price").ToString()
        book.Publish_date = myDataRow("publish_date").ToString()
        book.Description = myDataRow("description").ToString()
        books.Add(book)
    Next

    Dim serializer As JavaScriptSerializer = New JavaScriptSerializer
    Dim jsonString As String = serializer.Serialize(books)
    context.Response.Write(jsonString)

End Sub