gpt4 book ai didi

Scala 遍历一个对象

转载 作者:行者123 更新时间:2023-12-04 19:33:04 27 4
gpt4 key购买 nike

我通过这条线得到一个对象:

val product_array:Option[Any] = scala.util.parsing.json.JSON.parseFull(products_json)

打印出来的对象是这样的:

(product1Id,Map(product_picture -> https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/IPhone_3G.png/250px-IPhone_3G.png, product_price -> 299.99, recipient_picture -> https://www.allianz.com/static-resources/de/presse/mediendatenbank/people/v_1338807733000/zimerer_portrait_small_326x217.jpg, product_amount_gifted -> 100.00, recipient_username -> jDoe, product_name -> iPhone 3G, recipient_id -> 12345))(product2Id,Map(product_picture -> https://upload.wikimedia.org/wikipedia/en/thumb/7/7c/1stGen-iPad-HomeScreen.jpg/220px-1stGen-iPad-HomeScreen.jpg, product_price -> 399.99, recipient_picture -> https://image1.masterfile.com/em_w/05/11/94/400-05119409w.jpg, product_amount_gifted -> 200.00, recipient_username -> MJohnson, product_name -> iPad, recipient_id -> 67890)) 

如何遍历数组/对象以提取键和值?

更新:

当我尝试遍历对象时,它将空白值发送到键和值,如下所示:

for((key, value) <- product_array)     {       

}

这是原始的json:

{
"product1Id":{
"product_name":"iPhone 3G",
"product_price":"299.99",
"product_amount_gifted":"100.00",
"product_picture":"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/7\/73\/IPhone_3G.png\/250px-IPhone_3G.png",
"recipient_picture":"https:\/\/www.allianz.com\/static-resources\/de\/presse\/mediendatenbank\/people\/v_1338807733000\/zimerer_portrait_small_326x217.jpg",
"recipient_username":"jDoe",
"recipient_id":"12345"
},
"product2Id":{
"product_name":"iPad",
"product_price":"399.99",
"product_amount_gifted":"200.00",
"product_picture":"https:\/\/upload.wikimedia.org\/wikipedia\/en\/thumb\/7\/7c\/1stGen-iPad-HomeScreen.jpg\/220px-1stGen-iPad-HomeScreen.jpg",
"recipient_picture":"https:\/\/image1.masterfile.com\/em_w\/05\/11\/94\/400-05119409w.jpg",
"recipient_username":"MJohnson",
"recipient_id":"67890"
}
}

最佳答案

首先,请注意 parseFull返回 Option ,这意味着你的第一步是检查它是否包含某些东西(例如发生解析错误),我建议你为此使用模式匹配:

products match {
case Some(p) => println("Products found")
case None => println("Error parsing JSON")
}

如果您的选项包含实际值,它将被放置在 p 中多变的。接下来,请注意 parseFull 返回了 Option[Any] , 所以 pAny也。它应该在迭代之前被转换:

p.asInstanceOf[Map[String,Map[String,Any]]]

现在您可以遍历 map ,让我们使用 for :

for {
(id, desc) <- p.asInstanceOf[Map[String,Map[String,Any]]]
(propName, propValue) <- desc
} println(propName + "=" + propValue)

看似单循环,实际上是双循环。它遍历外部 map ,并进入每个内部 map 。在循环体内,您可以访问 id , desc , propNamepropValue变量。这可能是您正在寻找的。

整个脚本:

import scala.util.parsing.json.JSON._

val json = """{"product1Id":{"..."recipient_id":"67890"}}"""

val products = parseFull(json)

products match {
case Some(p) =>
for {
(id, desc) <- p.asInstanceOf[Map[String,Map[String,Any]]]
(propName, propValue) <- desc
} println(propName + "=" + propValue)

case None => println("Error parsing JSON")
}

祝你好运。

关于Scala 遍历一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com