Scala:使用jackson fasterxml访问json节点的值

时间:2015-05-21 01:23:57

标签: json scala

好的,我有一个scala方法如下:这个程序试图捕获Scala代码后粘贴的JSON doc中的值: 该值位于以下元素中#34;

    "patterns": [
          1
      ],


我无法捕获这个值。模式键或数组具有单个数字。它看起来不像是一把钥匙。如果不是,它是什么,我怎么能达到它。什么是正确的方法是JsonNode杰克逊API?

到目前为止,我认为以下代码行可以解决问题。

tPolicyNode.findValuesAsText("patterns").asScala.toList

显然,事实并非如此。如果有人能指出我出错的地方或给我一些指示,我可以实现我捕捉这个价值的目标。


import java.io._
import scala.collection.immutable.List
import javax.json.Json
import com.fasterxml.jackson.core._
import com.fasterxml.jackson.databind._
import java.util.Properties

object scalaProgram {

   val patternsPath = Array("tPolicyEntry", "comp", "comps", "patterns")


   //raw json (sample.json)coming in from another method
    def locateJsonValueInTree(rawJson: String) = {
        val rootNode = jsonMapper.readTree(rawJson)
        println("Root Node " + rootNode);

       //invoke method to find out the particular value in the json
       val listContainingValue = findtpEntryPattern(rootNode)
        // derive the value out of the list listContainingValue
       //This is supposed to be the value coming out of the 'patterns node'
      //on inspecting the value in this List, there is NOTHING in it
    }


     def findtpEntryPattern (rootNode: JsonNode): List[String] = {
          println("Entering findtpEntryPattern")
          var tPolicyNode = jn
          for(i <- 0 until patternsPath.length -1) {
             triggeringPolicyNode = tPolicyNode.path(patternsPath(i))
           }
         if(tPolicyNode.isMissingNode) {
           throw new RuntimeException("No node with policy Pattern found.")
         }

         //Return a List of String; but this List contains a single entry
           println("Triggering Policy ID : " + tPolicyNode.findValuesAsText("patterns"))
          //I am trying to capture the value contained in the patterns [ 1 ] //element in the JSON 
         tPolicyNode.findValuesAsText("patterns").asScala.toList

   }
}

&#13;
&#13;
"tPEntry": {
        "action": "Log",
        "comp": {
            "type": "OrComp",
            "components": [
                {
                    "type": "PatternComp",
                    "patterns": [
                        1
                    ],
                    "not": false
                }
            ],
            "not": false
        },
        "ticketInfo": {
            "genTicket": true,
            "tags": [],
            "viewUnseeableIds": [
                {
                    "type": "ApUserGroup",
                    "name": "Admins",
                    "description": "Group for system admins",
                    "admins": [
                        "153"
                    ],
                    "members": [],
                    "id": "04"
                }
            ]
        },
        "notInfo": {
            "genNotify": true,
            "contNames": [
                "me@example.com"
            ]
        }
    }
&#13;
&#13;
&#13;

0 个答案:

没有答案