解析JSON上使用Scala节点 - [英] Parsing nodes on JSON with Scala -

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

问题描述

我一直在问解析一个JSON文件,以获得所有超过用户inputed指定的速度总线。

I've been asked to parse a JSON file to get all the buses that are over a specified speed inputed by the user.

JSON文件可以是<一个href=\"https://www.dropbox.com/sh/op64cwvn41kjiip/AAChz1H3iK5ShoDzLKtfQsfEa/courses/c-bigdata/rj_onibus_gps.json?dl=0\"相对=nofollow>这里下载

The JSON file can be downloaded here

是这样的:

{
"COLUMNS": [
    "DATAHORA",
    "ORDEM",
    "LINHA",
    "LATITUDE",
    "LONGITUDE",
    "VELOCIDADE"
],
"DATA": [
    [
        "04-16-2015 00:00:55",
        "B63099",
        "",
        -22.7931,
        -43.2943,
        0
    ],
    [
        "04-16-2015 00:01:02",
        "C44503",
        781,
        -22.853649,
        -43.37616,
        25
    ],
    [
        "04-16-2015 00:11:40",
        "B63067",
        "",
        -22.7925,
        -43.2945,
        0
    ],
]
}

问题是:我是真正的新Scala和我从来没有使用JSON(可耻的是我)工作过。我需要的是从数据节点获得ORDEM,Linha和Velocidade。

The thing is: I'm really new to scala and I have never worked with json before (shame on me). What I need is to get the "Ordem", "Linha" and "Velocidade" from DATA node.

我创建了一个案例类enclousure所有的数据,以便以后寻找那些谁是在指定的速度。

I created a case class to enclousure all the data so as to later look for those who are over the specified speed.

case class Bus(ordem: String, linha: Int, velocidade: Int)

我这样做是读取该文件作为一个文本文件和劈裂。尽管这样,我需要预见该文件​​的内容,以去DATA节点之后的行。

I did this reading the file as a textFile and spliting. Although this way, I need to foreknow the content of the file in order to go to the lines after DATA node.

我想知道如何使用JSON解析器来做到这一点。我试过很多解决方案,但我无法适应我的问题,因为我需要提取一个节点内从数据节点的节点,而不是所有行。

I want to know how to do this using a JSON parser. I've tried many solutions, but I couldn't adapt to my problem, because I need to extract all the lines from DATA node instead of nodes inside one node.

谁能帮我?

PS:对不起,我的英语不是母语

PS: Sorry for my english, not a native speaker.

推荐答案

首先,你需要了解不同的JSON数据类型。基本类型JSON是数字,字符串,布尔值,数组和对象。在你的例子返回的数据与两个键的对象: DATA 。在键具有的值是字符串和数字数组。在 DATA 键具有值是一个字符串数组的数组。

First of all, you need to understand the different JSON data types. The basic types in JSON are numbers, strings, booleans, arrays, and objects. The data returned in your example is an object with two keys: COLUMNS and DATA. The COLUMNS key has a value that is an array of strings and numbers. The DATA key has a value which is an array of arrays of strings.

您可以使用库像PlayJSON与这种类型的数据的工作:

You can use a library like PlayJSON to work with this type of data:

val js = Json.parse(x).as[JsObject]
val keys = (js \ "COLUMNS").as[List[String]]
val values = (js \ "DATA").as[List[List[JsValue]]]

val busses = values.map(valueList => {
    val keyValues = (keys zip valueList).toMap 
    for {
      ordem <- keyValues("ORDEM").asOpt[String]
      linha <- keyValues("LINHA").asOpt[Int]
      velocidade <- keyValues("VELOCIDADE").asOpt[Int]
    } yield Bus(ordem, linha, velocidade)
})

请注意使用 asOpt 的转换属性预期类型时。这个操作符的键值转换为提供的类型(如果可能包裹在部分),并返回其它。所以,如果你想提供一个默认值,而不是忽略了其他的结果,你可以使用键值(LINHA)。asOpt [INT] .getOrElse(0),为例子。

Note the use of asOpt when converting the properties to the expected types. This operator converts the key-values to the provided type if possible (wrapped in Some), and returns None otherwise. So, if you want to provide a default value instead of ignoring other results, you could use keyValues("LINHA").asOpt[Int].getOrElse(0), for example.

您可以阅读更多关于这里使用的,例如 \\ 播放JSON方法和 asOpt 他们的文档。

You can read more about the Play JSON methods used here, like \ and as, and asOpt in their docs.

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

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