简体   繁体   English

Scala Play Json JSResultException错误API

[英]Scala Play Json JSResultException Error API

I am trying to get a nested value from a json content I get from an API Rest (Swagger). 我试图从我从API Rest(Swagger)获得的json内容中获取嵌套值。 I want to access to the first "url" value: 我想访问第一个“ url”值:

  val getFeedVersionURL =  url("https://api.transitfeeds.com/v1/getFeedVersions?").GET <<? List(
    "key" -> someKey,
    "feed" -> "consorcio-regional-de-transportes-de-madrid/743",
    "page" -> "1",
    "limit" -> "10000",
    "err" -> "1 (default)",
    "warn" ->"1 (default)")

  val response : Future[String] = Http.configure(_ setFollowRedirects true)(getFeedVersionURL OK as.String)

  val src: String = getFeedVersionURL.toString()

  response onComplete {
    case Success(content) => {
      println("Successful response" + content)

      //Parse the content
      val jsonObject = Json.parse(content)
      //Get url value
      val fileDownloadURL = (jsonObject \ "results" \ "versions" \ "url").as[String]
      //Save the file
      new URL(fileDownloadURL) #> new File("C:/Users//Desktop//file.zip") !!


    }
    case Failure(t) => {
      println("An error has occured: " + t.getMessage)
    }
  }

This is the json I receive: 这是我收到的json:

{
  "status": "OK",
  "ts": 1513600944,
  "results": {
    "total": 4,
    "limit": 100,
    "page": 1,
    "numPages": 1,
    "versions": [
      {
        "id": "consorcio-regional-de-transportes-de-madrid/743/20171127",
        "f": {
          "id": "consorcio-regional-de-transportes-de-madrid/743",
          "ty": "gtfs",
          "t": "Metro Ligero de Madrid GTFS",
          "l": {
            "id": 167,
            "pid": 166,
            "t": "Madrid, Spain",
            "n": "Madrid",
            "lat": 40.416775,
            "lng": -3.70379
          },
          "u": {
            "i": "http://crtm.maps.arcgis.com/home/item.html?id=aaed26cc0ff64b0c947ac0bc3e033196"
          }
        },
        "ts": 1511848526,
        "size": 403388,
        "url": "https://transitfeeds.com/p/consorcio-regional-de-transportes-de-madrid/743/20171127/download",
        "d": {
          "s": "20170106",
          "f": "20180629"
        },
        "err": [],
        "warn": []
      },
      {
        "id": "consorcio-regional-de-transportes-de-madrid/743/20170927",
        "f": {
          "id": "consorcio-regional-de-transportes-de-madrid/743",
          "ty": "gtfs",
          "t": "Metro Ligero de Madrid GTFS",
          "l": {
            "id": 167,
            "pid": 166,
            "t": "Madrid, Spain",
            "n": "Madrid",
            "lat": 40.416775,
            "lng": -3.70379
          },
          "u": {
            "i": "http://crtm.maps.arcgis.com/home/item.html?id=aaed26cc0ff64b0c947ac0bc3e033196"
          }
        },
        "ts": 1506554131,
        "size": 403052,
        "url": "https://transitfeeds.com/p/consorcio-regional-de-transportes-de-madrid/743/20170927/download",
        "d": {
          "s": "20170106",
          "f": "20180925"
        },
        "err": [],
        "warn": []
      },
      {
        "id": "consorcio-regional-de-transportes-de-madrid/743/20170526",
        "f": {
          "id": "consorcio-regional-de-transportes-de-madrid/743",
          "ty": "gtfs",
          "t": "Metro Ligero de Madrid GTFS",
          "l": {
            "id": 167,
            "pid": 166,
            "t": "Madrid, Spain",
            "n": "Madrid",
            "lat": 40.416775,
            "lng": -3.70379
          },
          "u": {
            "i": "http://crtm.maps.arcgis.com/home/item.html?id=aaed26cc0ff64b0c947ac0bc3e033196"
          }
        },
        "ts": 1495816290,
        "size": 408985,
        "url": "https://transitfeeds.com/p/consorcio-regional-de-transportes-de-madrid/743/20170526/download",
        "d": {
          "s": "20170106",
          "f": "20180526"
        },
        "err": [],
        "warn": []
      },
      {
        "id": "consorcio-regional-de-transportes-de-madrid/743/20161012",
        "f": {
          "id": "consorcio-regional-de-transportes-de-madrid/743",
          "ty": "gtfs",
          "t": "Metro Ligero de Madrid GTFS",
          "l": {
            "id": 167,
            "pid": 166,
            "t": "Madrid, Spain",
            "n": "Madrid",
            "lat": 40.416775,
            "lng": -3.70379
          },
          "u": {
            "i": "http://crtm.maps.arcgis.com/home/item.html?id=aaed26cc0ff64b0c947ac0bc3e033196"
          }
        },
        "ts": 1476308287,
        "size": 420670,
        "url": "https://transitfeeds.com/p/consorcio-regional-de-transportes-de-madrid/743/20161012/download",
        "d": {
          "s": "20160101",
          "f": "20170621"
        },
        "err": [],
        "warn": []
      }
    ]
  }
}

The problem is I receive this error: 问题是我收到此错误:

play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray())))))

Any ideas? 有任何想法吗?

cchantep in the comment is correct. cchantep在评论中是正确的。 You'll need to pull out the versions key and deal with it appropriately. 您需要拉出versions密钥并对其进行适当处理。 Here's some Very rough code that does what you want: 这是一些非常粗糙的代码,可以满足您的需求:

import play.api.libs.json._
val jsonObject = Json.parse(s) // s is that string you gave in your q

val fileDownloadURL: String = (jsonObject \ "results" \ "versions") match {
    case JsDefined(JsArray(versions)) => versions.headOption match {
        case Some(jsValue) => (jsValue \  "url").as[String]
        case _ => "NOPE hope you got some logic here"
    }
    case nope => throw new IllegalArgumentException("handle this better than this!")
}

And I get: 我得到:

res0: String = https://transitfeeds.com/p/consorcio-regional-de-transportes-de-madrid/743/20171127/download

Note that I did this using play 2.4 in my console, and for 2.3 and other versions there may be differences. 请注意,我是在控制台上使用play 2.4来完成此操作的,而对于2.3和其他版本,可能会有所不同。 If you provide the version of play you're using I can probably give you some more rough ideas about how to accomplish what you're up to. 如果您提供您正在使用的游戏版本,我可能会给您一些有关如何完成自己的工作的粗略想法。

Note that the above with the matching is one way, another way would be to use as a bunch 请注意,上面的与匹配项是一种方法,另一种方法是将其as一堆

val thisToo = (jsonObject \ "results" \ "versions").as[JsArray].value.headOption.map(first => (first \ "url").as[String]).getOrElse(throw new IllegalArgumentException("handle this better than this!"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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