尝试从Json Web响应中获取Json节点时出错

时间:2013-04-29 12:54:05

标签: java json jackson

我创建了一个简单的Spring Web MVC应用程序,它从酒店api获取json web响应,但是当我尝试从根节点使用findPath()方法时,我收到错误。 我正在使用杰克逊图书馆进行json解析。 这是代码:

ObjectMapper m = new ObjectMapper();
JsonNode rootNode = m.readTree(in);
JsonNode hotelNameNode = rootNode.findPath("HotelList");
String hotelName = hotelNameNode.path("name").getTextValue();

第3行产生错误。

以下是我的网络回复:

{
    "HotelListResponse": {
        "cachedSupplierResponse": {
            "@cachedTime": "0",
            "@candidatePreptime": "111",
            "@matchedCurrency": "true",
            "@matchedLocale": "true",
            "@otherOverheadTime": "4",
            "@supplierRequestNum": "211",
            "@supplierResponseNum": "20",
            "@supplierResponseTime": "405",
            "@tpidUsed": "5001"
        },
        "cacheKey": "302c317:13443ffb599:-7712",
        "cacheLocation": "10.186.168.61:7302",
        "customerSessionId": "0ABAA83D-2C31-7913-4432-FFB599907714",
         "HotelList": {
            "@activePropertyCount": "237",
            "@size": "1",
            "HotelSummary": {
                "@ubsScore": "1867",
                "@order": "0",
                "hotelId": 127092,
                "name": "The Edgewater - A Noble House Hotel",
                "address1": "Pier 67, 2411 Alaskan Way",
                "city": "Seattle",
                "stateProvinceCode": "WA",
                "postalCode": 98121,
                "countryCode": "US",
                "airportCode": "SEA",
                "supplierType": "E",
                "propertyCategory": 1,
                "hotelRating": 4,
                "confidenceRating": 85,
                "amenityMask": 6259019,
                "tripAdvisorRating": 4,
                "tripAdvisorReviewCount": 590,
                "tripAdvisorRatingUrl": "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif",
                "locationDescription": "Near Washington State Convention & Trade Center",
                "shortDescription": "<p><b>Location. </b> <br />The Edgewater - A Noble House Hotel is a business-friendly hotel located in central Seattle, close to Odyssey - The Maritime Discovery Center, Washington State Convention &",
                "highRate": 249,
                "lowRate": 186.75,
                "rateCurrencyCode": "USD",
                "latitude": 47.61252,
                "longitude": -122.35013,
                "proximityDistance": 11.898841,
                "proximityUnit": "MI",
                "hotelInDestination": true,
                "thumbNailUrl": "/hotels/1000000/20000/11200/11133/11133_73_t.jpg",
                "deepLink": "http://travel.ian.com/index.jsp?pageName=hotAvail&cid=55505&hotelID=127092&mode=2&numberOfRooms=2&room-0-adult-total=1&room-0-child-total=1&room-0-child-0-age=3&room-1-adult-total=1&room-1-child-total=1&room-1-child-0-age=5&arrivalMonth=8&arrivalDay=4&departureMonth=8&departureDay=5&showInfo=true&locale=en_US&currencyCode=USD",
                "RoomRateDetailsList": {
                    "RoomRateDetails": {
                        "roomTypeCode": 1160,
                        "rateCode": 1221260,
                        "maxRoomOccupancy": 2,
                        "quotedRoomOccupancy": 2,
                        "minGuestAge": 0,
                        "roomDescription": "City Lodge - Nonrefundable",
                        "promoId": 200803780,
                        "promoDescription": "7-Day Advance Purchase Special (Nonrefundable)",
                        "currentAllotment": 10,
                        "propertyAvailable": true,
                        "propertyRestricted": false,
                        "expediaPropertyId": 11133,
                        "rateKey": "f3525aff-9f4d-4d92-bc1c-144628fcaa30",
                        "nonRefundable": true,
                        "RateInfos": {
                            "@size": "1",
                            "RateInfo": {
                                "@rateChange": "false",
                                "@promo": "true",
                                "@priceBreakdown": "true",
                                "RoomGroup": {
                                    "Room": [
                                        {
                                            "numberOfAdults": 1,
                                            "numberOfChildren": 1,
                                            "childAges": 3
                                        },
                                        {
                                            "numberOfAdults": 1,
                                            "numberOfChildren": 1,
                                            "childAges": 5
                                        }
                                    ]
                                },
                                "ChargeableRateInfo": {
                                    "@commissionableUsdTotal": "373.5",
                                    "@total": "441.74",
                                    "@surchargeTotal": "68.24",
                                    "@nightlyRateTotal": "373.5",
                                    "@averageBaseRate": "249.0",
                                    "@averageRate": "186.75",
                                    "@maxNightlyRate": "186.75",
                                    "@currencyCode": "USD",
                                    "NightlyRatesPerRoom": {
                                        "@size": "1",
                                        "NightlyRate": {
                                            "@promo": "true",
                                            "@rate": "186.75",
                                            "@baseRate": "249.0"
                                        }
                                    },
                                    "Surcharges": {
                                        "@size": "1",
                                        "Surcharge": {
                                            "@amount": "68.24",
                                            "@type": "TaxAndServiceFee"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

错误讯息:

  

HTTP状态500 - 处理程序处理失败;嵌套异常是   java.lang.NoSuchMethodError:   org.codehaus.jackson.JsonNode.findPath(Ljava /郎/字符串;)Lorg / Codehaus的/杰克逊/ JsonNode;

Maven依赖项:

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-lgpl</artifactId>
  <version>1.9.12</version>
</dependency>
<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-mapper-asl</artifactId>
 <version>1.5.0</version>
</dependency>

1 个答案:

答案 0 :(得分:1)

您可能需要更新jackson-mapper-asl,现在最新版本为1.9.9 see MVNRepository}:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.9</version>
</dependency>