如何使用scala从多个级别的json中获取数据

时间:2014-11-28 09:38:10

标签: scala

我想从json中获取数据,但我无法在scala中编写代码,我只能在php中编写它,因为它使用起来非常简单,但我不知道我怎么能做同样的事情在斯卡拉。请帮帮我。

{
    "home": {
        "type": "literal",
        "options": {
            "route": "\/",
            "defaults": {
                "controller": "Apiv1\\Controller\\Index",
                "action": "index"
            }
        }
    },
    "praise": {
        "type": "literal",
        "options": {
            "route": "\/apiv1\/praise",
            "defaults": {
                "controller": "Apiv1\\Controller\\Praise",
                "action": "index"
            }
        },
        "may_terminate": true,
        "child_routes": {
            "status": {
                "type": "literal",
                "options": {
                    "route": "\/status",
                    "defaults": {
                        "action": "status"
                    }
                }
            }
        }
    },
    "admin": {
        "type": "literal",
        "options": {
            "route": "\/admin",
            "defaults": {
                "controller": "Admin\\Controller\\Index",
                "action": "index"
            }
        },
        "may_terminate": true,
        "child_routes": {
            "routes": {
                "type": "literal",
                "options": {
                    "route": "\/routes",
                    "defaults": {
                        "controller": "Admin\\Controller\\Routes",
                        "action": "index"
                    }
                },
                "may_terminate": true,
                "child_routes": {
                    "list": {
                        "type": "literal",
                        "options": {
                            "route": "\/list",
                            "defaults": {
                                "action": "list"
                            }
                        }
                    }
                }
            }
        }
    }
}

我想从json中选择路线字段,我想列出所有路线

这是我的php版本功能

function parseRoute($a, $pre = '', &$urls)
{
    foreach ($a as $k => $v) {
        $route = $pre . $v['options']['route'];
        $urls[] = $route;
        if (isset($v['child_routes']) && is_array($v['child_routes'])) {
            $this->parseRoute($v['child_routes'], $route, $urls);
        }
        $route = null;
    }
}
$urls = array();
var_dump(parseRoute(json_decode($data), '', $urls));

1 个答案:

答案 0 :(得分:0)

最好的办法是为Scala使用一个现有的JSON库。以下是描述您的选择的相关问题:

What JSON library to use in Scala?

接下来是使用Lift-JSON的示例。看到链接,有很多例子。您可以这样做:

import net.liftweb.json._

val rawJson = """{
   "home":{
      "type":"literal",
      "options":{
         "route":"/",
         "defaults":{
            "controller":"Apiv1\\Controller\\Index",
            "action":"index"
         }
      }
   },
   "praise":{
      "type":"literal",
      "options":{
         "route":"/apiv1/praise",
         "defaults":{
            "controller":"Apiv1\\Controller\\Praise",
            "action":"index"
         }
      },
      "may_terminate":true,
      "child_routes":{
         "status":{
            "type":"literal",
            "options":{
               "route":"/status",
               "defaults":{
                  "action":"status"
               }
            }
         }
      }
   },
   "admin":{
      "type":"literal",
      "options":{
         "route":"/admin",
         "defaults":{
            "controller":"Admin\\Controller\\Index",
            "action":"index"
         }
      },
      "may_terminate":true,
      "child_routes":{
         "routes":{
            "type":"literal",
            "options":{
               "route":"/routes",
               "defaults":{
                  "controller":"Admin\\Controller\\Routes",
                  "action":"index"
               }
            },
            "may_terminate":true,
            "child_routes":{
               "list":{
                  "type":"literal",
                  "options":{
                     "route":"/list",
                     "defaults":{
                        "action":"list"
                     }
                  }
               }
            }
         }
      }
   }
}"""

val json = parse(rawJson)

val routeList = json \\ "route"    
// which returns a structure like this:
JObject(List(JField(route,JString(/)), JField(route,JString(/apiv1/praise)), JField(route,JString(/status)), JField(route,JString(/admin)), JField(route,JString(/routes)), JField(route,JString(/list))))
// which is a JSON object

// if you want a list of routes as strings as in your example extract them this way now:
val routeList: List[String] = routeObject \\ classOf[JString]
// which returns:
List[String] = List(/, /apiv1/praise, /status, /admin, /routes, /list)

请注意,我假设路径可以在JSON doc中的任何嵌套级别(您可能希望强制执行'route'的某个级别或父级)。我还假设所有路由都是字符串而忽略那些不是 - 如果你想要更多选项,你可以使用JValue的衍生类来模式匹配路由类型:JArray,{{1} },JString