JSon-Scala-解析列表 [英] JSon-Scala - Parse list

查看:311
本文介绍了JSon-Scala-解析列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have problem parsing JSon into RDD

{"data":"{\" orderID \:\" 3 \,\" products \:[{\" productID \:10028,\" category \:\" 342 \, \名称\":\儿童外套\",\性别\":\儿童\",\运动\":\篮球\",\颜色\":\蓝色\",\ 零售价格":268.0,销售价格":268.0,销售数量":1,应税价格":268.0,品牌":"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 \":\骑车\",\颜色\":\蓝色\",\零售价格":59.0,\销售价格\":59.0,\销售数量\":6,\应纳税价格\:59.0,\"品牌\:\" 361体育\,\"股票数量:994,"小计:354.0,\"祖先类别:[\" 2426 \,\" 241 \,\" 33 \,\" 24 \,\" 2429 \,\" 334 \,\" 2439 \,\" 21 \,\" 3 \,\" 2 \",\"1 \",\"2412 \",\"2503 \",\"2451 \"]}}

{"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\"]}}

当我将此信息读入RDD时,

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)

在处理类别时,我在第9行出现错误 我想像列表一样将JSON的'ancestorCategories'放入'category'或RDD中 列表(2426、2454、241、342、24、34、2439、21、3、2、1、2412、2430、2503)

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)

错误:找到:列表[org.json4s.JsonAST.JValue] [错误] [控制台$] [错误]必需:Array [String]

Error: found : List[org.json4s.JsonAST.JValue] [ERROR] [Console$] [error] required: Array[String]

有人可以帮助我从List [org.json4s.JsonAST.JValue]转换为List [String]吗? 非常感谢.

Can anyone help me to convert from List[org.json4s.JsonAST.JValue] to List[String]? Thank you very much.

推荐答案

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

我认为这可能可行,因为您可以在x => {} bolock中尽一切可能

I think this might works , as you can do what ever you can in the x=>{} bolock

这篇关于JSon-Scala-解析列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆