当前位置: 首页 > 知识库问答 >
问题:

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

金高轩
2023-03-14

我已经创建了一个简单的Spring Web MVC应用程序,该应用程序从酒店API获取json Web响应,但是当我尝试从根节点使用findPath()方法时,我收到错误。我正在使用Jackson Library进行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 - 处理程序处理失败;nested exception is java.lang.NoSuchMethodError: org.codehaus.jackson.JsonNode.findPath(Ljava/lang/String;)Lorg/codehaus/jackson/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个答案

雍阳
2023-03-14

您可能需要更新 jackson-mapper-asl,它现在升级到 1.9.9 版本(请参阅 MVNRepository):

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.9</version>
</dependency>
 类似资料:
  • 我正在为我的web应用程序实现azure,并尝试通过下面的openId connect教程获得访问令牌 内容类型:application/x-www-form-urlencoded GRANT_TYPE=授权代码 client_id=2D4D11A2-F814-46A7-890A-274A72A7309E 请帮帮我 提前致谢

  • 我是iOS和Swift的新手,我正在尝试使用AlamoFire3.4.0来做一个web请求。当我的请求成功时,一切都很好。但是,如果我的请求失败,服务器将返回300或更大的状态代码,以及响应体中的一些JSON,其中包含关于请求失败原因的更多信息。例如,我正在与之交谈的API要求对每个请求进行身份验证。如果身份验证由于某种原因失败,我将返回401,响应体中的JSON将为: 我发出此请求的代码如下所示

  • 问题内容: 我正在尝试从Web读取JSON数据,但是该代码返回空结果。我不确定我在做什么错。 问题答案: 理想的方法 不是 使用,而是直接在阅读器上使用解码器。这是一个不错的函数,它获取url并将其响应解码到结构上。 使用示例: 您不应该在生产中使用默认结构,如最初回答的那样!(/ etc调用的是哪个)。原因是默认客户端没有设置超时。如果远程服务器无响应,那将是糟糕的一天。

  • 问题内容: 我想从Http get响应中获取一个对象: 这是我当前对Http get的代码: 这是convertSteamToString函数: 现在,我只是得到一个字符串对象。我如何找回JSON对象。 问题答案: 您获得的字符串只是JSON Object.toString()。这意味着您将获取JSON对象,但使用String格式。 如果应该获取JSON对象,则可以输入:

  • 我想从Http get响应中获取一个对象: 这是我当前的Http get代码: 这是转换的SteamToString函数: 现在我只是得到一个字符串对象。如何取回JSON对象。

  • 我已经使用API从API中提取了JSON响应,请放心,它看起来像这样: 现在,我实际的JSON响应在JSON数组中有数千个JSON对象,一些键有空值,例如“secondKey”在一些JSON对象中有空值。我需要获取JSON响应中所有空值的键。对我该如何做有什么想法吗? 我解决这个问题的想法是使用Jackson库反序列化JSON并获取所有空值。然而,考虑到性能,是否有任何有效的解决方案?