在Scala中解析JSON文件



我有一个JSON文件:

JsonString="""{"schema":{"type":"struct","name":"emp_table","fields":[{"field":"emp_id","type":"string"},{"field":"emp_name","type":"String"},{"field":"city","type":"string"},{"field":"emp_sal","type":"string"},{"field":"manager_name","type":"string"},]},"payload":{"emp_id":"1","emp_name":"abc","city":"NY","emp_sal":"100000","manager_name":"xyz"}}"""

我想解析并将值放入 Array(1,abc,NY,100000,xyz)

我在Scala中使用了Lift JSON API:

case class Emp(val emp_id: String, val emp_name: String, val city: String, val emp_sal: String, val manager_name: String)
val stocks = ArrayBuffer[Emp]()
val json = JsonParser.parse(EmpString)
val elements = (json \ "payload").children
for ( acct <- elements ) {
val stock = acct.extract[Emp]
stocks += stock
}
stocks.toArray
}

但它给了我

Array[Emp] = Array(Emp(1,abc,NY,100000,xyz))

任何人都可以指导我解析此JSON文件,也许使用其他API(例如Gson或Jackson)?

如果您有Array[Emp],并且想要Array[String],则可以随时仅flatMap()

empArr.flatMap(_.productIterator).map(_.toString)
//res0: Array[String] = Array(1, abc, NY, 100000, xyz)

相关内容

  • 没有找到相关文章

最新更新