Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在scala中从json中提取数据_Java_Json_Scala_Url - Fatal编程技术网

Java 在scala中从json中提取数据

Java 在scala中从json中提取数据,java,json,scala,url,Java,Json,Scala,Url,比如说,我想在第一行提取“SearchResult”的值。我在stackoverflow上寻找了一些提示,但我感到困惑,我是Scala的新手。有人能提出一个简单而直接的方法吗。感谢使用播放json在这里查找 您可以执行以下操作来提取searchResult 解析json val payload=Json.parse(str) 使用\\运算符获取所需的键值 { "findItemsByKeywordsResponse": [{ "ack": ["Success"],

比如说,我想在第一行提取“SearchResult”的值。我在stackoverflow上寻找了一些提示,但我感到困惑,我是Scala的新手。有人能提出一个简单而直接的方法吗。感谢使用
播放json

在这里查找

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

  • 解析json

    val payload=Json.parse(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"]
        }]
    }
    
  • Scala REPL 使用circe解析器(另请参见如何使用它/向您的
    build.sbt添加什么
    ),以下代码片段将打印
    右侧(273088613147)
    ,这是
    搜索结果中的
    项目ID

    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...
    

    或者,您可以为JSON编写一个case类,然后为其编写解码器/编码器,但我想,这不是您所要求的。

    可能是重复的,非常感谢您的帮助。这真的很有帮助,也很容易理解。:)
    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...
    
    import io.circe._, io.circe.parser._
    
    def myParse(jsonString: String) {
      val result = parse(jsonString) match {
        case Left(failure) => println("Invalid JSON :(")
        case Right(json) => {
          val cursor = json.hcursor
          println(cursor.downField("findItemsByKeywordsResponse").downArray.
                  downField("searchResult").downArray.
                  downField("item").downArray.
                  downField("itemId").downArray.as[String])
        }
      }
    }