问题
I have problem parsing JSon into RDD
{"data":"{\"orderID\":\"3\",\"products\":[{\"productID\":10028,\"category\":\"342\",\"name\":\"Kids Coats\",\"gender\":\"Kids\",\"sport\":\"Basketball\",\"color\":\"Blue\",\"retailPrice\":268.0,\"sellPrice\":268.0,\"sellQuantity\":1,\"taxablePrice\":268.0,\"brand\":\"Inno Fashion\",\"stockQuantity\":999,\"subTotal\":268.0,\"ancesstorCategories\":[\"2426\",\"2454\",\"241\",\"342\",\"24\",\"34\",\"2439\",\"21\",\"3\",\"2\",\"1\",\"2412\",\"2430\",\"2503\"]},{\"productID\":10031,\"category\":\"334\",\"name\":\"Kids Tshirt\",\"gender\":\"Kids\",\"sport\":\"Cycling\",\"color\":\"Blue\",\"retailPrice\":59.0,\"sellPrice\":59.0,\"sellQuantity\":6,\"taxablePrice\":59.0,\"brand\":\"361 Sports\",\"stockQuantity\":994,\"subTotal\":354.0,\"ancesstorCategories\":[\"2426\",\"241\",\"33\",\"24\",\"2429\",\"334\",\"2439\",\"21\",\"3\",\"2\",\"1\",\"2412\",\"2503\",\"2451\"]}}
When I read this information into RDD,
1. val content = parse(event.properties.get[String]("data"))
2. val productID = (for {JInt(x) <- (content \\ "productID")} yield x.toString())
3. val sellProductQuantity = (for {JInt(x) <- (content \\ "sellQuantity")} yield x.toString())
4. val category = for { JArray(x) <- (content \\ "ancesstorCategories")} yield x
5. val compactProductId = compact(content \\ "productID")
6. yield BuyEvent(
7. user = userID,
8. item = productID(index).toString,
9. category = category(index),
10. count = (sellProductQuantity(index).values.toString).toInt)
I got an error at line 9, when processing category I would like to get the 'ancestorCategories' of JSON into the 'category' or RDD like a list List(2426, 2454, 241, 342, 24, 34, 2439, 21, 3, 2, 1, 2412, 2430, 2503)
Error: found : List[org.json4s.JsonAST.JValue] [ERROR] [Console$] [error] required: Array[String]
Can anyone help me to convert from List[org.json4s.JsonAST.JValue] to List[String]? Thank you very much.
回答1:
you could use render to get raw String out of Value
scala> val json = ("name" -> "joe")
scala> compact(render(json))
res1: String = {"name":"joe"}
thus you could:
val category =
for { JArray(x) <- (content \\ "ancesstorCategories")}
yield compact(render(x))
I can not run it locally now ,hope it helps.
回答2:
var content = parse(json)
var aarray = ArrayBuffer()
(content \\ "ancesstorCategories").children.foreach(x=>{
aarray+x.toString
})
var list= aarray.toList
I think this might works , as you can do what ever you can in the x=>{} bolock
来源:https://stackoverflow.com/questions/33013037/json-scala-parse-list