-3

我在将 JSon 解析为 RDD 时遇到问题

{"data":"{\"orderID\":\"3\",\"products\":[{\"productID\":10028,\"category\":\"342\",\"name \":\"童装\",\"性别\":\"童装\",\"运动\":\"篮球\",\"颜色\":\"蓝色\",\"零售价\ ":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\":\ "自行车\",\"颜色\":\"蓝色\",\"retailPrice\":59.0,\"sellPrice\":59.0,\"sellQuantity\":6,\"taxablePrice\":59.0,\ "品牌\":\"361 Sports\",\"stockQuantity\":994,\"subTotal\":354.0,\"ancesstorCategories\":[\"2426\",\"241\",\"33 \",\"24\",\"2429\",\"334\",\"2439\",\"21\",\"3\",\"2\",\"1\" ,\"2412\",\"2503\",\"2451\"]}}性别\":\"儿童\",\"运动\":\"自行车\",\"颜色\":\"蓝色\",\"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\"]}}性别\":\"儿童\",\"运动\":\"自行车\",\"颜色\":\"蓝色\",\"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\"]}}\"brand\":\"361 Sports\",\"stockQuantity\":994,\"subTotal\":354.0,\"ancesstorCategories\":[\"2426\",\"241\",\" 33\",\"24\",\"2429\",\"334\",\"2439\",\"21\",\"3\",\"2\",\"1\ ",\"2412\",\"2503\",\"2451\"]}}\"brand\":\"361 Sports\",\"stockQuantity\":994,\"subTotal\":354.0,\"ancesstorCategories\":[\"2426\",\"241\",\" 33\",\"24\",\"2429\",\"334\",\"2439\",\"21\",\"3\",\"2\",\"1\ ",\"2412\",\"2503\",\"2451\"]}}

当我将这些信息读入 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)

我在第 9 行遇到错误,在处理类别时,我想将 JSON 的“ancestorCategories”放入“类别”或 RDD,如列表 List(2426, 2454, 241, 342, 24, 34, 2439, 21, 3、2、1、2412、2430、2503)

错误:找到:List[org.json4s.JsonAST.JValue] [ERROR] [Console$] [error] required: Array[String]

谁能帮我从 List[org.json4s.JsonAST.JValue] 转换为 List[String]?非常感谢。

4

2 回答 2

0

您可以使用渲染从值中获取原始字符串

scala> val json = ("name" -> "joe")
scala> compact(render(json))
res1: String = {"name":"joe"}

因此你可以:

val category =
  for { JArray(x) <- (content \\ "ancesstorCategories")}
  yield compact(render(x))

我现在无法在本地运行它,希望它有帮助。

于 2015-10-08T10:57:25.447 回答
0
var content = parse(json)
var aarray = ArrayBuffer()
(content \\ "ancesstorCategories").children.foreach(x=>{
aarray+x.toString
})
var list= aarray.toList

我认为这可能有效,因为您可以在 x=>{} 块中做任何事情

于 2015-10-08T13:07:30.427 回答