JSon-Scala - 解析列表

时间:2015-10-08 10:23:19

标签: json scala predictionio

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

{ “数据”: “{\” 的orderID \ “:\” 3 \” \ “产品\”:[{\ “的productID \”:10028,\ “类别\”:\ “342 \”, “名字”:“儿童外套”,“性别”:“儿童”,“运动”:“篮球”,“颜色”:“蓝色”, “retailPrice”:268.0,“sellPrice”:268.0,“sellQuantity”:1,“taxablePrice”:268.0,“品牌”:“Inno Fashion”,“stockQuantity”: 999,\ “小计\”: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 \”,\ “运动\”:\ “自行车\”,\ “颜色\”:\ “蓝\”,\ “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 \”]}}

当我将此信息读入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'变成'category'或RDD就像列表一样 清单(2426,2454,241,342,24,34,2439,21,3,2,1,2412,2430,2503)

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

任何人都可以帮我从List [org.json4s.JsonAST.JValue]转换为List [String]吗? 非常感谢你。

2 个答案:

答案 0 :(得分:0)

你可以使用render来获取超出值的原始字符串

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

因此你可以:

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

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

答案 1 :(得分:0)

var content = parse(json)
var aarray = ArrayBuffer()
(content \\ "ancesstorCategories").children.foreach(x=>{
aarray+x.toString
})
var list= aarray.toList

我认为这可能有效,因为你可以在x =&gt; {} bolock中做任何事情