使用Scala中的自定义键映射

时间:2013-06-06 10:05:23

标签: scala

有一些数据的字符串表示:

"jsonData": {
      "data1": {
        "field1": "data1",
        "field2": 1.0,
        "field3": true
      },
      "data211": {
        "field1": "data211",
        "field2": 4343.0,
        "field3": false
      },
      "data344": {
        "field1": "data344",
        "field2": 436778.51,
        "field3": true
      },
      "data41": {
        "field1": "data41",
        "field2": 14348.0,
        "field3": true
      }
    }

如何在Scala中表示它?我以为我可能是

Map[(String, Double, Boolean), String]

type KeyValueType = (String, Double, Boolean)
Map[KeyValueType, String]

但是,它给了我错误的信息:

error: missing arguments for method apply in class GenMapFactory;
follow this method with `_' if you want to treat it as a partially applied function

而且,我不确定它是否是正确的代表。

那么我如何表示它,如果我的方法是正确的,我该如何摆脱错误?

1 个答案:

答案 0 :(得分:1)

关于您的错误,您可能只需要添加()来调用apply方法,因为只有具有类型参数的对象名称(Map)没有意义。

我建议不要使用元组来保存数据。它们被初学者过度使用。请改用一个班级。像

这样的东西
case class MyDataType(field1: String, field2: Double, field3: Boolean)

然后,您将数据读入Vector[MyDataType]