Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

recursion - Recursively extracting JSON field values in Groovy

I need to implement a method that will scan a string of JSON for a particular targetField and either return the value of that field (if it exists), or null (if it doesn't):

// Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'fizz') => 'buzz'
// Ex: extractFieldValue(/{ "fizz" : "buzz" }/, 'foo') => null
String extractFieldValue(String json, String targetField) {
    // ...
}

This solution has to be recursive and work at any nesting-level in the (hierarchical) JSON string. Also it needs to work for JSON arrays as well.

My best attempt so far:

String extractFieldValue(String json, String targetField) {
    def slurper = new JsonSlurper()
    def jsonMap = slurper.parseText(json)

    jsonMap."${targetField}"
}

This only works on top-level (non-nested) JSON fields. I asked the Google Gods how to use JsonSlurper recursively, but couldn't find anything useful. Any ideas here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given this input string in a variable called json:

{
    "a":"a",
    "b":{"f":"f", "g":"g"},
    "c":"c",
    "d":"d",
    "e":[{"h":"h"}, {"i":{"j":"j"}}],
}

This script:

import groovy.json.JsonSlurper

def mapOrCollection (def it) {
    it instanceof Map || it instanceof Collection
}

def findDeep(def tree, String key) {
    switch (tree) {
        case Map: return tree.findResult { k, v ->
            mapOrCollection(v)
                ? findDeep(v, key)
                : k == key
                    ? v
                    : null
        }
        case Collection: return tree.findResult { e ->
            mapOrCollection(e)
                ? findDeep(e, key)
                : null
        }
        default: return null
    }
}

('a'..'k').each { key ->
    def found = findDeep(new JsonSlurper().parseText(json), key)
    println "${key}: ${found}"
}

Gives these results:

a: a
b: null
c: c
d: d
e: null
f: f
g: g
h: h
i: null
j: j
k: null

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...