使用PHP提取JSON输出以逐行获取密钥对值

时间:2019-01-31 14:51:59

标签: php json

我想使用PHP提取JSON输出并尝试以下代码,但我正在输出单个字符而不是按行值。我读过类似的文章,但未能获取密钥对值和以单个字符的形式输出。无论字段值不存在,都可以使用null。有人可以帮我逐行获取各个键的输出吗?

尝试输入的代码:

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line);
    foreach ($someArray as $key => $value) {
        echo $value["key"] . ", " . $value["status"] . "<br>";
  }
}

someArray输出:

Array ( [key] => XYZ-6680 [status] => Open [components] => API [currentVersion] => Release1.2 [expectedVersion] => Array ( ) [customerInfo] => Default Calendar when the delegate responds to those invitations. ) Array ( [key] => XYZ-3325 [status] => Closed [components] => API [currentVersion] => Release 1.0 [expectedVersion] => Array ( [0] => Array ( [self] => https://bug.restify.com/rest/api/2/version/27771 [id] => 27771 [name] => Release1.2 [archived] => [released] => ) ) [customerInfo] => Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039]) ) Array ( [key] => XYZ-223 [status] => Closed [components] => API [currentVersion] => Release 1.3 [expectedVersion] => Array ( [0] => Array ( [self] => https://bug.restify.com/rest/api/2/version/29171 [id] => 29171 [name] => Release1.2 [archived] => [released] => ) ) [customerInfo] => "Default Calendar" user preference, `zimbraPrefDefaultCalendarId`. ) 

行输出:

{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."} X, X
O, O
A, A
R, R
,
D, D
{"key":"XYZ-3325","status":"Closed","components":"API","currentVersion":"Release 1.0","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/27771","id":"27771","name":"Release1.2","archived":false,"released":false}],"customerInfo":"Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039])"} X, X
C, C
A, A
R, R
,
F, F
{"key":"XYZ-223","status":"Closed","components":"API","currentVersion":"Release 1.3","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/29171","id":"29171","name":"Release1.2","archived":false,"released":false}],"customerInfo":"\"Default Calendar\" user preference, `zimbraPrefDefaultCalendarId`."}X, X
C, C
A, A
R, R
,
", "

JSON(resultFile内容):

{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."}
{"key":"XYZ-3325","status":"Closed","components":"API","currentVersion":"Release 1.0","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/27771","id":"27771","name":"Release1.2","archived":false,"released":false}],"customerInfo":"Fixed a number of bugs related to DA: * CrossMailboxSearch: Group label was shown even when all setting items of the group were hidden on New Account dialog ([https://bug.goog.restify/show_bug.cgi?id=1542 Bug 1542]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://bug.goog.restify/show_bug.cgi?id=1039 Bug 1039])"}
{"key":"XYZ-223","status":"Closed","components":"API","currentVersion":"Release 1.3","expectedVersion":[{"self":"https://bug.restify.com/rest/api/2/version/29171","id":"29171","name":"Release1.2","archived":false,"released":false}],"customerInfo":"\"Default Calendar\" user preference, `zimbraPrefDefaultCalendarId`."}

预期输出:

键,状态,组件,currentVersion,expectedVersion,customerInfo的行值。

实际输出:

Output

3 个答案:

答案 0 :(得分:3)

首先,@ Curious_mind关于强制将json_decode的输出作为具有第二个参数的true的关联数组是正确的。然后我认为您应该通过直接回显$ key和$ value来获得所需的内容,

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line,true); 
    foreach ($someArray as $key => $value) {
        echo key . ", " . $value . "<br/>";
  }
}

但是要小心,如果$ value是一个数组,则会出现错误(不能只是回显一个数组),因此您需要可处理地处理由json生成的数组。

我修改了此处找到的功能:Echo a multidimensional array in PHP 并添加了一些测试以显示布尔值的字符串。

它应根据需要显示json值:

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    //echo $line;
    $someArray = json_decode($line,true); 
    RecursiveWrite($someArray);
}

function RecursiveWrite($array) {
    foreach ($array as $key => $value) {

        echo $key .', ';

        if(is_array($value)) {
            echo "<br>";
            RecursiveWrite($value);
        }
        elseif(is_bool($value)) {
            echo ($value? 'true' : 'false') . "<br>"; 
        }
        else {
            echo $value . "<br>";
        }
    }
}

答案 1 :(得分:1)

$someArray = json_decode($line,true);怎么样?因为没有第二个参数truejson_decode()会以object的形式返回结果,在这种情况下,您必须使用$value->key来访问非$value['key']的键

while (!feof($resultFile)) {
    $line = fgets ($resultFile);
    echo $line;
    $someArray = json_decode($line,true); # see the tweak here
    foreach ($someArray as $key => $value) {
        echo $value["key"] . ", " . $value["status"] . "<br/>";
  }
}

答案 2 :(得分:1)

首先,我同意Dexter0015的回答。应该将其标记为正确答案,因为它将处理各种各样的结果。

我只是想投入2美分,以解决针对用户问题的较短且非常严重的问题。

/* Load up a record that is json encoded */
$json = '{"key":"XYZ-6680","status":"Open","components":"API","currentVersion":"Release1.2","expectedVersion":[],"customerInfo":"Default Calendar when the delegate responds to those invitations."}';
/* Use json_decode to convert the JSON string to a PHP Array. Be sure and set the second parameter to true in json_decode to get an array and not an object */
$array = json_decode($json, true);

/*
 * Now iterate through the result extracting the key and value of the array created from json decode 
 * There could be instances (expectedVersion) that may have an array of values. So test to see
 * if the array value is an array. If so using print_r, otherwise just echo out the $value as a string
 */
foreach ($array as $key => $value) {
    if (!is_array($value)) {
        echo '* Key ' . $key . ' has a value of :' . $value . PHP_EOL;
    } else {
        echo "* Key " . $key . ' has an array of values :' . print_r($value, true) . PHP_EOL;
    }
}