PHP从JSON递归检索值

时间:2014-01-30 09:09:35

标签: php json recursion

我从cURL API获取JSON对象,我需要递归地浏览JSON并打印数组的树视图。

以下是JSON对象:

{"message":"OK",
"records":{"Company INC":
    [{"positionName":"CEO",
      "seniorName":"John Doe",
      "seniorId":"1035",
      "seniorSex":"male",
      "child":[{"positionName":"Assistant to CEO",
                "seniorName":"Jane Doe",
                "seniorId":"427",
                "seniorSex":"female",
                "child":[{"positionName":"Assitant to assistant",
                          "seniorName":"James Doe",
                          "seniorId":"1370",
                          "seniorSex":"male"},
                         {"positionName":"2nd Assistant",
                          "seniorName":"Jana D. OE",
                          "seniorId":"1049",
                          "seniorSex":"female","child": ...

等。正如您在记录部分所看到的,有些人有孩子,我也需要打印它们。 这是我在json_decode(true)JSON对象之后使用的函数。

function recurseTree($var){
    $out = '<li>';
    foreach($var as $v){
        if(is_array($v)){
            $out .= '<ul>'.recurseTree($v).'</ul>';
        }else{
            $out .= $v." ";
        }
    }
    return $out.'</li>';
}

这很好用,除了它打印JSON的所有信息,我想在一行只打印seniorName和positionName。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

使用PHP函数in_array()跳过某组属性。或者用它来定义你不想跳过的键:

$keys_to_skip = array('some', 'keys', 'to', 'skip');

function recurseTree($var, $keys_to_skip){
    $out = '<li>';
    foreach($var as $k => $v){ // Note I take the key here
        if(in_array($k, $keys_to_skip)) { continue; }

        if(is_array($v)){
            $out .= '<ul>'.recurseTree($v, $keys_to_skip).'</ul>';
        } else {
            $out .= $v." ";
        }
    }

    return $out.'</li>';
}

答案 1 :(得分:-1)

public static JsonNode getValuefromJsonRecursively(String jsonString, String jsonKey) {
        JsonNode result = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            JsonNode object = mapper.readValue(jsonString, JsonNode.class);
            result = getValuefromJsonRecursively(object, jsonKey);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    public static JsonNode getValuefromJsonRecursively(JsonNode input, String jsonKey) {

        JsonNode result = null;
        if (input instanceof ObjectNode) {
            Iterator<String> keys = input.fieldNames();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                JsonNode value = input.get(key);
                if (key.equals(jsonKey)) {
                    result= value;
                    break;
                }
                if (value instanceof ArrayNode) {
                    result= getValuefromJsonRecursively((ArrayNode) value, jsonKey);
                }
                else if (value.isObject()) {
                    result= getValuefromJsonRecursively((ObjectNode) value, jsonKey);
                } 
            }
        } else if (input instanceof ArrayNode) {
            input = (ArrayNode) input;
            int arraySize = (input).size();
            for (int i = 0; i < arraySize; i++) {
                JsonNode a = (input).get(i);
                if (a instanceof ArrayNode) {
                    result= getValuefromJsonRecursively((ArrayNode) a, jsonKey);
                }
                else if (a.isObject()) {
                    result= getValuefromJsonRecursively((ObjectNode) a, jsonKey);
                } 
            }
        }
        return result;
    }