使用PHP提取JSON输出以逐行获取密钥对值 [英] Extract JSON ouput to get line by line key pair values using PHP

查看:85
本文介绍了使用PHP提取JSON输出以逐行获取密钥对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to extract JSON ouput using PHP and tried below code but I'm getting output of single character instead of row wise values. I read similar posts but failed to get key pair values and getting output as single character. Wherever field values won't present, null is ok for that. How can I get output line by line for respective keys?

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的逐行值.

Expected output:

Line by line values of key, status, components, currentVersion, expectedVersion, customerInfo.

推荐答案

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

First of all, @Curious_mind is right about forcing output of json_decode as an associative array with second parameter to true. Then I think you should get what you want by echoing directly $key and $value, like so:

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

但是要小心,如果$ value是一个数组,则会收到一个错误(不能只是回显一个数组),因此您需要处理由json产生的数组.

BUT, careful, if $value is an array, you will get an error (can't just echo an array), so you need to handle the array resulting of you json recusivly.

我修改了此处找到的函数:在PHP中回显多维数组 并添加了一些测试以显示字符串的布尔值.

I adapt the function found here: Echo a multidimensional array in PHP and added some testing to display string for boolean value too.

它应该根据需要显示json值:

It should display the json values as you wish:

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>";
        }
    }
}

这篇关于使用PHP提取JSON输出以逐行获取密钥对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆