输⼊⽂件数据为json格式,
{"bid": "1","品牌":"奥迪","⼚商":"⼀汽-⼤众奥迪","csid":"5"}
topspeed{"bid": "1","品牌":"奥迪","⼚商":"奥迪(进⼝)","csid":"63"}
{"bid": "1","品牌":"奥迪","⼚商":"奥迪RS","csid":"276"}
众泰m300{"bid": "56","品牌":"阿斯顿·马丁","⼚商":"阿斯顿·马丁","csid":"81"}
{"bid": "56","品牌":"阿斯顿·马丁","⼚商":"AMR", "csid":"349"}
{"bid": "78","品牌":"阿尔法·罗密欧","⼚商":"阿尔法·罗密欧","csid":"71"}
{"bid": "239","品牌":"艾康尼克","⼚商":"艾康尼克ICONIQ Motors","csid":"353"}
{"bid": "224","品牌":"Alpina","⼚商":"Alpina","csid":"338"}
{"bid": "2","品牌":"宝马","⼚商":"华晨宝马","csid":"25"}
{"bid": "2","品牌":"宝马","⼚商":"宝马(进⼝)","csid":"59"}
输出⽂件数据为k-v格式,
276 品牌_奥迪⼚商_奥迪RS
5 品牌_奥迪⼚商_⼀汽-⼤众奥迪
马自达2两厢报价59 品牌_宝马⼚商_宝马(进⼝)
353 品牌_艾康尼克⼚商_艾康尼克ICONIQ Motors
71 品牌_阿尔法·罗密欧⼚商_阿尔法·罗密欧
25 品牌_宝马⼚商_华晨宝马
大众自造磁悬球形车81 品牌_阿斯顿·马丁⼚商_阿斯顿·马丁
338 品牌_Alpina⼚商_Alpina
63 品牌_奥迪⼚商_奥迪(进⼝)
349 品牌_阿斯顿·马丁⼚商_AMR
操作代码:
/**
* 将本地json字典⽂件转化为k-v格式(格式:sid 车级_中型车\u0001品牌_⼤众u0001)
* @param inFilePath 输⼊⽂件路径
* @param outFilePath 输出⽂件路径
*/
def proDicFromJson(inFilePath: String, outFilePath: String) {
val labelFields = List("车级", "品牌", "车系", "年款", "⼚商", "经销商", "车型报价", "最低价(万元)", "最⾼价(万元)", "省份", "地市", "关注点", "类别", "备注")
// 1.获取⽂件名
val filename = outFilePath.substring(outFilePath.lastIndexOf("/") + 1, outFilePath.indexOf("."))
// 2.读取json⽂件
val lines = Source.fromFile(inFilePath).getLines().toIterator
var dicMap: Map[String, String] = Map()
while (lines.hasNext) {
val labeljson = JSON.())
labeljson match {
//note1!
case Some(labelmap: Map[String, Any]) =>
var value = ""
labelFields.foreach { x =>
if (ains(x)) {
var tmpVal = labelmap(x)
value += x + "_" + labelmap(x) + "\u0001" //多个属性值使⽤"\u0001"分割
}长安之星新车价格
}
val key = labelmap(filename).toString()
dicMap += key -> value
case _ => String());
}
}
// 3..获取指定字段值并保存
val writer = new PrintWriter(new File(outFilePath))
dicMap.foreach { x =>
writer.write(x._1 + "\t" + x._2 + "\n")
}
writer.close()
println(filename + "转换成功!")
}
说明:
- ⽂件名要和json中key值相同。
重点:
note1:
辽宁省高速公路路况查询case Some(labelmap: Map[String, Any]) =>Any不能改为String!
case Some(labelmap: Map[String, String]) =>:满⾜json格式,且k-v是String-String。如果Any改为String,输⼊⽂件json数据value有可能不是String类型,就不会执⾏case Some⽅法,就会导致输出⽂件数据少于输⼊⽂件。
发布评论