使用不同的键:值对解析JSON字符串的特定部分的优雅方法是什么? (斯卡拉)

时间:2013-09-04 19:28:42

标签: regex json scala

我需要根据它的相关名称找到一个ID。 我的程序对API进行REST调用。 API以JSON格式返回结果。 名称是唯一的,所以我想用它来获取它的Id值。 请注意......可以包含任何内容,并且包含一些{Such} Id键。 Id可以嵌套在任意数量的{... {... {...} ...} ...}中 身份证总是紧跟在名字之前。

注意:...是出于隐私原因无法显示的代码。代码本身(在排除私有数据之前)是由Advanced Rest Client返回并在http://jsonlint.com/验证为有效JSON的REST调用的结果。

代码按原样返回:

{
  Id: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
  ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
  Name: "Automated Runs"
  OrderNumber: 0
  Expands: [3]
    0:  "Children"
    1:  "Parent"
    2:  "Project"
    ...
    scripts: [4]
    0:  {
      Id: "0b70a55c-5e68-4b27-bfcf-a22f00c5dc48"
      Name: "3816"
      PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
      ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
      ...
      Expands: [6]
      0:  "Assignments"
      1:  "Attachments"
      2:  "FieldControls"
      3:  "FieldValues"
      4:  "Package"
      5:  "Steps"
      ...
    1:  {
      Id: "14e5c663-0d5a-46bb-ac48-a22f00c15998"
      Name: "3814"
      PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
      ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
      ...
      Expands: [6]
      0:  "Assignments"
      1:  "Attachments"
      2:  "FieldControls"
      3:  "FieldValues"
      4:  "Package"
      5:  "Steps"
      ...
    2:  {
      Id: "00d52fcd-b611-4f69-aeb6-a22f00c263a9"
      Name: "3815"
      ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
      ...
      Expands: [6]
      0:  "Assignments"
      1:  "Attachments"
      2:  "FieldControls"
      3:  "FieldValues"
      4:  "Package"
      5:  "Steps"
      ...

    3:  {
      Id: "4d3a6132-8497-4b6b-a064-a22f00c669ff"
      Name: "3817"
      ...
      Expands: [6]
      0:  "Assignments"
      1:  "Attachments"
      2:  "FieldControls"
      3:  "FieldValues"
      4:  "Package"
      5:  "Steps"
      ...
}

我尝试过的东西包括正则表达式(我是新手,遇到麻烦)和简单的字符串拆分。虽然我有字符串拆分工作,但它是半硬编码的。

我想要的是:

def getID(myJSON:String, myName:String){
  val pattern = "\"Id\": \"*\",\r\n\"Name\":\"" + myName + "\",\""
  get the id (*) from result using pattern
}

或者甚至更好地将其转换为通用。

def getID(myJSON:String, myValue:String, searchKey:String, findKey:String){
  val pattern = { ... findKey: *...} in the inner most  { ... searchKey: * ...} scope
  get the id (*) from result using the pattern in the found {...searchKey...} scope
}

要么是伟大的,也非常感激。 我目前的代码如下:

result.split("Id\": \"")(3).split("\"")(0)

它可能很漂亮,但它有很多不幸的空间。可能由用户创建Id,该计数器将计数设置为不正确等等......

谢谢你, 埃里克斯通

1 个答案:

答案 0 :(得分:1)

如何在json4s

中使用for comprehension
scala> :paste
// Entering paste mode (ctrl-D to finish)

  import org.json4s._
  import org.json4s.native.JsonMethods._

  val json = """
      {
        "a": {
          "Id": "1",
          "Name": "Name1",
          "b": {
            "Id": "2",
            "Name": "Name2",
          }
        }
      }
    """


  def getId(json: String, name: String) = {
    val res = for {
      JObject(child) <- parse(json)
      JField("Name", JString(n)) <- child
      JField("Id", JString(id)) <- child
      if n == name
    } yield id

    res.headOption
  }

// Exiting paste mode, now interpreting.

scala> getId(json, "Name1")
res4: Option[String] = Some(1)

scala> getId(json, "Name2")
res5: Option[String] = Some(2)