简体   繁体   中英

Rails - Replacing text in JSON hash with gsub

Using Rails 4.1, I would like to clean up some data in a JSON response, for example & to & & to & , etc.

I've been trying to use gsub, but running into issues because the JSON returned is not a string.

Here's my code, which returns an error: undefined method `gsub' for #

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)

Here is an example of the JSON response:

{
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"
                        }
                    }
                }
            }
        }
    }
}

}

Have you tried changing the hotel_data variable into a string before using gsub?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM