我有这些数据:
{"id":"P-0SE01606VF925501Y2UAKG3Y","state":"CREATED","name":"Monthly subscription to Paypal","description":"Subscribtion.","type":"FIXED","payment_definitions":[{"id":"PD-35U317461H38251442UAKG4A","name":"Regular Payment Definition","type":"REGULAR","frequency":"Month","amount":{"currency":"NOK","value":"500"},"cycles":"12","charge_models":[{"id":"CHM-7T021625H451740052UAKG4A","type":"SHIPPING","amount":{"currency":"NOK","value":"0"}},{"id":"CHM-313690493W320615U2UAKG4A","type":"TAX","amount":{"currency":"NOK","value":"0"}}],"frequency_interval":"2"}],"merchant_preferences":{"setup_fee":{"currency":"NOK","value":"0"},"max_fail_attempts":"0","return_url":"http:\/\/www.vg.no","cancel_url":"http:\/\/www.yahoo.no","auto_bill_amount":"YES","initial_fail_amount_action":"CONTINUE"},"create_time":"2017-01-25T09:41:45.967Z","update_time":"2017-01-25T09:41:45.967Z","links":[{"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/billing-plans\/P-0SE01606VF925501Y2UAKG3Y","rel":"self","method":"GET"}]}
如何从中提取val data = List(Map(event_id -> ABC, event_name -> visited, timestamp -> 1478187513, member_id -> 111, category -> web, field1 -> abc),
Map(event_id -> DEF, event_name -> added, timestamp -> 1478187520, member_id -> 111),
Map(event_id -> ABC, event_name -> visited, timestamp -> 1478187522, member_id -> 111, category -> web, field1 -> abc),
Map(event_id -> ABC, event_name -> visited, timestamp -> 1478187618, member_id -> 111, category -> web, field1 -> abc))
和member_id
?问题是所有条目中都存在field1
,而member_id
则不存在。
我尝试了这个,但它没有编译说field1
:
Type mismatch: expected String, actual Int
答案 0 :(得分:1)
我认为这会奏效:
data.collect{
case map if map.keySet.contains(field1) => (map(member_id),map(field1))
}.headOption
它将返回Option[(String, String)]
。我假设1)始终有一个member_id
,member_id
都是一样的,你只关心第一个field1
。如果不是这种情况,这将抛出异常。
此外,如果您确定至少有一个符合条件的条目,您还可以直接提取字段:
val (memberId, fieldOne) = data.collect{
case map if map.keySet.contains(field1) => (map(member_id),map(field1))
}.head
同样,如果没有与初始集合中的条件匹配的值,head
将抛出异常。
编辑以回复最新评论:
val (memberId, fieldOne) = data.collect{
case map if map.keySet.contains(field1) => (map(member_id),map(field1))
}.headOption.getOrElse((data.head(member_id),""))
这将返回fieldOne=""
,memberId
设置为正确的值,假设初始List
中至少有一个条目。但这非常脆弱。