0

I want to pick up data from the json, but i can not code it in scala, i can only write it in php, because it is very simple to use, but i have no idea how can i do the same thing in scala. please help me out.

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

I want to pick up the route field from the json, and i would like to list all routes

here is my php version function

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));
bash0r
  • 774
  • 6
  • 17
莫子虚
  • 1
  • 2
  • Ehm... The second code sample is no valid scala. You might want to fix your tags or example. – bash0r Nov 28 '14 at 09:41
  • Try to use http://json4s.org/ library for such issues. There is obvious singleline solution, just look at examples. – hellraiser Nov 28 '14 at 10:17

1 Answers1

0

The best thing to do is to use one of the existing JSON libraries for Scala. Here is related question describing your options:

What JSON library to use in Scala?

Next is an example of using Lift-JSON. See the link, there are many examples. Here is what you can do:

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)

Note that I'm assuming that routes can be at any level of nesting in JSON doc (you might want to enforce certain level or parent of the 'route'). I also assume that all routes are Strings and ignore the ones which are not - if you want more options you can pattern match on the type of route using derivative classes of JValue: JArray, JString, JObject, etc.

Community
  • 1
  • 1
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65