从json解码字段打印单个值

时间:2014-02-07 15:05:49

标签: php json symfony get echo

如何打印json解码数组的各个值?

$monrow = json_encode($monrow);
$monrow= preg_replace("/_DOT_/",  ".", $monrow);
$monrow = json_decode($monrow,true);

我尝试了以下内容:

var_dump($monrow);  //success, prints the whole array
echo $monrow->name;   //fails with: Notice: Trying to get property of non-object
var_dump($monrow->name);  //fails: same error message
var_dump($monrow->properties);  //fails: properties is of type object or hash

由于

3 个答案:

答案 0 :(得分:2)

json_encode()会将一些对象或数组转换为JSON。 json_decode($ var,true)会将其转换为ARRAY。 要让对象再次使用json_decode($ var)而不使用true。

答案 1 :(得分:1)

尝试获取数组的值,而不是对象的属性:

var_dump($monrow);
echo $monrow['name'];
var_dump($monrow['name']);

您可以检查$monrow是数组还是对象:

if (is_array($monrow)) {
    echo $monrow['name'];
} elseif (is_object($monrow)) {
    echo $monrow->name;
} else {
    // do something else...
}

在您的情况下,请尝试使用foreach

foreach ($monrow as $row) {
    if (is_array($row)) {
        // do something with array
        echo $row['name']; // for example
    } elseif (is_object($row)) {
        // do something with object
        echo $row->name; // for example
    } else {
        // maybe do something else if you need...
    }
}

通常在json_decode之后您拥有数组或stdClass的实例。

答案 2 :(得分:0)

我不知道Victor的答案在哪里,但这就是你应该如何处理json_decode:

$decodeToArray = true;
$monrow = json_encode($monrow);
$monrow= preg_replace("/_DOT_/",  ".", $monrow);
$monrow = json_decode($monrow, $decodeToArray);

$parsedArray = array();
foreach ($monrow as $key => $value) {
    if ($decodeToArray) {
        if (is_array($monrow[$key]) && $key == 'properties') {
            $obj = new Object();
            foreach ($monrow[$key] as $property => $contents) {
                $obj->$property = $contents;
            }
            $parsedArray[$key] = $obj;
        } else {
            $parsedArray[$key] = $value;
        }
    } else {
        if (is_object($monrow->$key) && $key == 'properties') {
            $obj = new Object();
            foreach ($monrow->$key as $property => $contents) {
                $obj->$property = $contents;
            }
            $parsedArray[$key] = $obj;
        } else {
            $parsedArray[$key] = $value;
        }
    }
}

对于Victor,json_decode($ json,true)将递归地将整个JSON字符串更改为数组,返回的数组中将没有对象:

print_r(json_decode('{"array":[1,2,3],"boolean":true,"null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"objectArray":[{"a":"b","c":"d","e":"f"},{"a":"b","c":"d","e":"f"},{"a":"b","c":"d","e":"f"}],"string":"Hello World"}', true));

Array
(
    [array] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [boolean] => 1
    [null] => 
    [number] => 123
    [object] => Array
        (
            [a] => b
            [c] => d
            [e] => f
        )

    [objectArray] => Array
        (
            [0] => Array
                (
                    [a] => b
                    [c] => d
                    [e] => f
                )

            [1] => Array
                (
                    [a] => b
                    [c] => d
                    [e] => f
                )

            [2] => Array
                (
                    [a] => b
                    [c] => d
                    [e] => f
                )

        )

    [string] => Hello World
)