我通过这条线得到一个对象:
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"
}
}