Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
Arrays 玩Scala JSON对象处理_Arrays_Json_Scala_Playframework_Functional Programming - Fatal编程技术网

Arrays 玩Scala JSON对象处理

Arrays 玩Scala JSON对象处理,arrays,json,scala,playframework,functional-programming,Arrays,Json,Scala,Playframework,Functional Programming,我从第三方API调用中获取JSON对象。 假设它看起来像这样: { "view": [{ "width": "2100", "height": "1575", "code": "1", "href": "1.png" }, { "width": "320", "height": "240", "code": "2", "href": "2.png" }, {

我从第三方API调用中获取JSON对象。 假设它看起来像这样:

{
  "view": [{
      "width": "2100",
      "height": "1575",
      "code": "1",
      "href": "1.png"
    },
    {
      "width": "320",
      "height": "240",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "3",
      "href": "4.png"
    }
  ]
}

我希望遍历数组,找到与特定宽度、高度和代码匹配的对象,然后返回href。我对Scala还是很陌生,希望能得到一些帮助。谢谢

有多种方法可以做到这一点,例如:

import play.api.libs.json._

case class View(width: String, height: String, code: String, href: String)

object View { 
  implicit val viewFormat: Format[View] = Json.format[View] 
}

case class MyJsonObject(view: Seq[View])

object MyJsonObject { 
  implicit val myJsonObjectFormat: Format[MyJsonObject] = Json.format[MyJsonObject] 
}

val s = """{
  "view": [{
      "width": "2100",
      "height": "1575",
      "code": "1",
      "href": "1.png"
    },
    {
      "width": "320",
      "height": "240",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "3",
      "href": "4.png"
    }
  ]
}"""

def findMyHref(width: String, height: String, code: String): Option[String] = {
  for {
    myJsonObject <- Json.parse(s).asOpt[MyJsonObject]
    myView <- myJsonObject.view.find(v => v.width == width && v.height == height && v.code == code)
  } yield {
    myView.href
  }
}

findMyHref("2100", "1575", "2") //Some(2.png)

findMyHref("1", "2", "3") //None
import play.api.libs.json_
案例类视图(宽度:字符串、高度:字符串、代码:字符串、href:字符串)
对象视图{
隐式val viewFormat:Format[View]=Json.Format[View]
}
案例类MyJsonObject(视图:Seq[view])
对象MyJsonObject{
隐式val myJsonObjectFormat:Format[MyJsonObject]=Json.Format[MyJsonObject]
}
val s=”“”{
“视图”:[{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“1”,
“href”:“1.png”
},
{
“宽度”:“320”,
“高度”:“240”,
“代码”:“2”,
“href”:“2.png”
},
{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“2”,
“href”:“2.png”
},
{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“3”,
“href”:“4.png”
}
]
}"""
def findMyHref(宽度:字符串,高度:字符串,代码:字符串):选项[String]={
为了{

myJsonObject有多种方法可以做到这一点,例如:

import play.api.libs.json._

case class View(width: String, height: String, code: String, href: String)

object View { 
  implicit val viewFormat: Format[View] = Json.format[View] 
}

case class MyJsonObject(view: Seq[View])

object MyJsonObject { 
  implicit val myJsonObjectFormat: Format[MyJsonObject] = Json.format[MyJsonObject] 
}

val s = """{
  "view": [{
      "width": "2100",
      "height": "1575",
      "code": "1",
      "href": "1.png"
    },
    {
      "width": "320",
      "height": "240",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "2",
      "href": "2.png"
    },
    {
      "width": "2100",
      "height": "1575",
      "code": "3",
      "href": "4.png"
    }
  ]
}"""

def findMyHref(width: String, height: String, code: String): Option[String] = {
  for {
    myJsonObject <- Json.parse(s).asOpt[MyJsonObject]
    myView <- myJsonObject.view.find(v => v.width == width && v.height == height && v.code == code)
  } yield {
    myView.href
  }
}

findMyHref("2100", "1575", "2") //Some(2.png)

findMyHref("1", "2", "3") //None
import play.api.libs.json_
案例类视图(宽度:字符串、高度:字符串、代码:字符串、href:字符串)
对象视图{
隐式val viewFormat:Format[View]=Json.Format[View]
}
案例类MyJsonObject(视图:Seq[view])
对象MyJsonObject{
隐式val myJsonObjectFormat:Format[MyJsonObject]=Json.Format[MyJsonObject]
}
val s=”“”{
“视图”:[{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“1”,
“href”:“1.png”
},
{
“宽度”:“320”,
“高度”:“240”,
“代码”:“2”,
“href”:“2.png”
},
{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“2”,
“href”:“2.png”
},
{
“宽度”:“2100”,
“高度”:“1575”,
“代码”:“3”,
“href”:“4.png”
}
]
}"""
def findMyHref(宽度:字符串,高度:字符串,代码:字符串):选项[String]={
为了{

myJsonObject在阅读了文档之后,你自己已经尝试了什么?@cchantep我已经尝试了模式匹配,并试图像在JS中一样映射它,但是类型总是有一些问题。我对类型化语言世界是新的,只是在简单地先阅读文档时真的很沮丧。你已经尝试了什么阅读完文档后,请自己动手?@cchantep我已经尝试了模式匹配,并试图像在JS中一样映射它,但是类型总是有一些问题。我对类型化语言世界还不熟悉,只是简单地先阅读文档真的很令人沮丧