java - 在scala中从json中提取数据

标签 java json scala url

{
    "findItemsByKeywordsResponse": [{
        "ack": ["Success"],
        "version": ["1.13.0"],
        "timestamp": ["2018-02-28T09:52:48.736Z"],
        "searchResult": [{
            "@count": "1",
            "item": [{
                "itemId": ["273088613147"],
                "title": ["MTG Air Elemental *ALPHA*"],
                "globalId": ["EBAY-ENCA"],
                "primaryCategory": [{
                    "categoryId": ["38292"],
                    "categoryName": ["MTG Individual Cards"]
                }],
                "galleryURL": ["http:\/\/thumbs4.ebaystatic.com\/m\/mrUjH7CtVxARfthfKH3wYAw\/140.jpg"],
                "viewItemURL": ["http:\/\/www.ebay.com\/itm\/MTG-Air-Elemental-ALPHA-\/273088613147"],
                "paymentMethod": ["PayPal"],
                "autoPay": ["false"],
                "postalCode": ["M4S2H7"],
                "location": ["Canada"],
                "country": ["CA"],
                "shippingInfo": [{
                    "shippingServiceCost": [{
                        "@currencyId": "USD",
                        "__value__": "0.0"
                    }],
                    "shippingType": ["Free"],
                    "shipToLocations": ["Worldwide"],
                    "expeditedShipping": ["false"],
                    "oneDayShippingAvailable": ["false"],
                    "handlingTime": ["1"]
                }],
                "sellingStatus": [{
                    "currentPrice": [{
                        "@currencyId": "CAD",
                        "__value__": "25.0"
                    }],
                    "convertedCurrentPrice": [{
                        "@currencyId": "USD",
                        "__value__": "19.6"
                    }],
                    "sellingState": ["Active"],
                    "timeLeft": ["P29DT19H50M26S"]
                }],
                "listingInfo": [{
                    "bestOfferEnabled": ["false"],
                    "buyItNowAvailable": ["false"],
                    "startTime": ["2018-02-28T05:43:14.000Z"],
                    "endTime": ["2018-03-30T05:43:14.000Z"],
                    "listingType": ["FixedPrice"],
                    "gift": ["false"]
                }],
                "returnsAccepted": ["false"],
                "condition": [{
                    "conditionId": ["3000"],
                    "conditionDisplayName": ["Used"]
                }],
                "isMultiVariationListing": ["false"],
                "topRatedListing": ["false"]
            }]
        }],
        "paginationOutput": [{
            "pageNumber": ["1"],
            "entriesPerPage": ["100"],
            "totalPages": ["1"],
            "totalEntries": ["1"]
        }],
        "itemSearchURL": ["http:\/\/www.ebay.com\/sch\/i.html?_nkw=mtg+air+elemental+alpha&_ddo=1&_ipg=100&_pgn=1"]
    }]
}

我想在第一行中提取“SearchResult”的值。我在 stackoverflow 上寻找提示,但我感到困惑,而且我是 Scala 的新手。任何人都可以建议一种简单直接的方法来做到这一点。谢谢

最佳答案

使用 play-json 在这里找到 https://mvnrepository.com/artifact/com.typesafe.play/play-json_2.12/2.6.8

您可以执行以下操作来提取 searchResult

  1. 解析json

    val payload = Json.parse(str)

  2. 使用\\运算符获取所需的键值

    val payload = Json.parse(str)
    payload \\ "searchResult"
    

Scala REPL

scala> :paste
// Entering paste mode (ctrl-D to finish)

val str = """{"findItemsByKeywordsResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2018-02-28T09:52:48.736Z"],"searchResult":[{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http:\/\/thumbs4.ebaystatic.com\/m\/mrUjH7CtVxARfthfKH3wYAw\/140.jpg"],"viewItemURL":["http:\/\/www.ebay.com\/itm\/MTG-Air-Elemental-ALPHA-\/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"CAD","__value__":"25.0"}],"convertedCurrentPrice":[{"@currencyId":"USD","__value__":"19.6"}],"sellingState":["Active"],"timeLeft":["P29DT19H50M26S"]}],"listingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2018-02-28T05:43:14.000Z"],"endTime":["2018-03-30T05:43:14.000Z"],"listingType":["FixedPrice"],"gift":["false"]}],"returnsAccepted":["false"],"condition":[{"conditionId":["3000"],"conditionDisplayName":["Used"]}],"isMultiVariationListing":["false"],"topRatedListing":["false"]}]}],"paginationOutput":[{"pageNumber":["1"],"entriesPerPage":["100"],"totalPages":["1"],"totalEntries":["1"]}],"itemSearchURL":["http:\/\/www.ebay.com\/sch\/i.html?_nkw=mtg+air+elemental+alpha&_ddo=1&_ipg=100&_pgn=1"]}]}"""


// Exiting paste mode, now interpreting.

str: String = {"findItemsByKeywordsResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2018-02-28T09:52:48.736Z"],"searchResult":[{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http:\/\/thumbs4.ebaystatic.com\/m\/mrUjH7CtVxARfthfKH3wYAw\/140.jpg"],"viewItemURL":["http:\/\/www.ebay.com\/itm\/MTG-Air-Elemental-ALPHA-\/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShi...

scala> val payload = Json.parse(str)
payload: play.api.libs.json.JsValue = {"findItemsByKeywordsResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2018-02-28T09:52:48.736Z"],"searchResult":[{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http://thumbs4.ebaystatic.com/m/mrUjH7CtVxARfthfKH3wYAw/140.jpg"],"viewItemURL":["http://www.ebay.com/itm/MTG-Air-Elemental-ALPHA-/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"]...

scala> payload \\ "searchResult"
res7: Seq[play.api.libs.json.JsValue] = List([{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http://thumbs4.ebaystatic.com/m/mrUjH7CtVxARfthfKH3wYAw/140.jpg"],"viewItemURL":["http://www.ebay.com/itm/MTG-Air-Elemental-ALPHA-/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"currentPrice...

scala> val searchResult = payload \\ "searchResult"
searchResult: Seq[play.api.libs.json.JsValue] = List([{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http://thumbs4.ebaystatic.com/m/mrUjH7CtVxARfthfKH3wYAw/140.jpg"],"viewItemURL":["http://www.ebay.com/itm/MTG-Air-Elemental-ALPHA-/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"curr...

scala> searchResult
res8: Seq[play.api.libs.json.JsValue] = List([{"@count":"1","item":[{"itemId":["273088613147"],"title":["MTG Air Elemental *ALPHA*"],"globalId":["EBAY-ENCA"],"primaryCategory":[{"categoryId":["38292"],"categoryName":["MTG Individual Cards"]}],"galleryURL":["http://thumbs4.ebaystatic.com/m/mrUjH7CtVxARfthfKH3wYAw/140.jpg"],"viewItemURL":["http://www.ebay.com/itm/MTG-Air-Elemental-ALPHA-/273088613147"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["M4S2H7"],"location":["Canada"],"country":["CA"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"currentPrice...

关于java - 在scala中从json中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026665/

相关文章:

javascript - 通过ejs文件中的javascript访问JSON

javascript - 如何在 Jquery 中迭代具有多个对象的 json 字符串

Scala:指定公共(public)方法覆盖 protected 方法

Scala 调度示例

java - java comparable上的排序算法问题

java - 从 log4j 转换后的 Logback 模式异常

Java 泛型和数组

javascript - 所有主流浏览器都支持 JSON.parse 吗?

Scala 构造函数、逻辑和继承

java - 在 x-z 平面中翻转 gui 组件