Rails - 用gsub替换JSON哈希中的文本

时间:2014-07-06 20:54:06

标签: ruby-on-rails ruby json

使用Rails 4.1,我想清理JSON响应中的一些数据,例如& to &等。

我一直在尝试使用gsub,但因为返回的JSON不是字符串而遇到问题。

这是我的代码,它返回一个错误:未定义的方法`gsub'为#

api = Expedia::Api.new
response = api.get_list({:propertyName => hotel_name, :destinationString => hotel_city}) 
hotel_data = response.body
hotel_data = hotel_data.gsub('&', '&')
hotel_parsed = JSON.parse(hotel_data.to_json)

以下是JSON响应的示例:

{
customerSessionId: "0AB81CA6-0067-4913-C262-28568C903F10",
numberOfRoomsRequested: 1,
moreResultsAvailable: true,
cacheKey: "-70a00674:13c2628568c:-3f0f",
cacheLocation: "10.184.28.166:7301",
cachedSupplierResponse: {
    @supplierCacheTolerance: "MED_ENHANCED",
    @cachedTime: "0",
    @supplierRequestNum: "541",
    @supplierResponseNum: "1",
    @supplierResponseTime: "457",
    @candidatePreptime: "39",
    @otherOverheadTime: "6",
    @tpidUsed: "5001",
    @matchedCurrency: "true",
    @matchedLocale: "true"
},
HotelList: {
    @size: "1",
    @activePropertyCount: "1703",
    HotelSummary: {
        @order: "0",
        @ubsScore: "0",
        hotelId: 399395,
        name: "Hotel Pincio",
        address1: "Via Capo Le Case 50",
        city: "Rome",
        postalCode: "00187",
        countryCode: "IT",
        airportCode: "FCO",
        supplierType: "E",
        propertyCategory: 1,
        hotelRating: 3,
        confidenceRating: 52,
        amenityMask: 5275648,
        tripAdvisorRating: 4,
        tripAdvisorReviewCount: 70,
        tripAdvisorRatingUrl: "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif",
        locationDescription: "Near Trevi Fountain",
        shortDescription: "<p><b>Location.  </b> <br />Located in central Rome, Hotel  Pincio is within walking distance of Sistina Theater and Trevi Fountain.  Nearby points of interest also include Piazza Venezia and Vittorio",
        highRate: 190.78,
        lowRate: 190.78,
        rateCurrencyCode: "USD",
        latitude: 41.90391,
        longitude: 12.48489,
        proximityDistance: 7.134917,
        proximityUnit: "MI",
        hotelInDestination: true,
        thumbNailUrl: "/hotels/2000000/1250000/1241000/1240932/1240932_58_t.jpg",
        deepLink: "http://travel.ian.com/index.jsp?pageName=hotAvail&cid=55505&hotelID=399395&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalMonth=5&arrivalDay=9&departureMonth=5&departureDay=11&showInfo=true&locale=en_US&currencyCode=USD",
        RoomRateDetailsList: {
            RoomRateDetails: {
                roomTypeCode: 200135881,
                rateCode: 200796552,
                maxRoomOccupancy: 3,
                quotedRoomOccupancy: 2,
                minGuestAge: 0,
                roomDescription: "Standard Double",
                propertyAvailable: true,
                propertyRestricted: false,
                expediaPropertyId: 1240932,
                rateKey: "472738f4-4715-4939-93cc-2138ec6e1ee6",
                RateInfos: {
                    @size: "1",
                    RateInfo: {
                        @priceBreakdown: "true",
                        @promo: "false",
                        @rateChange: "false",
                        RoomGroup: {
                            Room: {
                                numberOfAdults: 2,
                                numberOfChildren: 0
                            }
                        },
                        ChargeableRateInfo: {
                            @averageBaseRate: "190.78",
                            @averageRate: "190.78",
                            @commissionableUsdTotal: "381.56",
                            @currencyCode: "USD",
                            @maxNightlyRate: "190.78",
                            @nightlyRateTotal: "381.56",
                            @surchargeTotal: "38.16",
                            @total: "419.72",
                            NightlyRatesPerRoom: {
                                @size: "2",
                                NightlyRate: {
                                    0: {
                                        @baseRate: "190.78",
                                        @rate: "190.78",
                                        @promo: "false"
                                    },
                                    1: {
                                        @baseRate: "190.78",
                                        @rate: "190.78",
                                        @promo: "false"
                                    }
                                }
                            },
                            Surcharges: {
                                @size: "1",
                                Surcharge: {
                                    @type: "TaxAndServiceFee",
                                    @amount: "38.16"
                                }
                            }
                        },
                        nonRefundable: true,
                        HotelFees: {
                            @size: "1",
                            HotelFee: {
                                @description: "MandatoryTax",
                                @amount: "10.49"
                            }
                        },
                        rateType: "MerchantStandard",
                        currentAllotment: 9
                    }
                },
                ValueAdds: {
                    @size: "2",
                    ValueAdd: {
                        0: {
                            @id: "2048",
                            description: "Free Wireless Internet"
                        },
                        1: {
                            @id: "16777216",
                            description: "Breakfast Buffet"
                        }
                    }
                }
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

您是否尝试在使用gsub之前将hotel_data变量更改为字符串?

api = Expedia::Api.new
response = api.get_list({:propertyName => hotel_name, :destinationString => hotel_city}) 
hotel_data = response.body
hotel_data = hotel_data.to_s.gsub('&', '&')
hotel_parsed = JSON.parse(hotel_data.to_json)