【问题标题】:Extract JSON ouput to get line by line key pair values using PHP使用 PHP 提取 JSON 输出以逐行获取密钥对值
【发布时间】:2019-01-31 14:51:59
【问题描述】:

我想使用 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(结果文件内容):

{"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`."}

预期输出:

key、status、components、currentVersion、expectedVersion、customerInfo 的逐行值。

实际输出:

【问题讨论】:

  • 我看到你有一行代码写着“echo $line”,结果在哪里?
  • 也添加了行输出。

标签: php json


【解决方案1】:

首先,@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>";
        }
    }
}

【讨论】:

  • 感谢@Dexter0015,您的回答已被接受。但是面临来自 API 的多行问题。你想让我打开单独的问题以避免这里混乱吗?你有什么建议?
  • 嗨@JiteshSojitra,请。
【解决方案2】:

$someArray = json_decode($line,true); 怎么样?因为没有第二个参数truejson_decode() 将返回结果为object,在这种情况下,您必须使用$value-&gt;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/>";
  }
}

【讨论】:

  • 感谢您的努力,但不幸的是,它正在按照帖子中添加的屏幕截图打印,带有单个字符。
【解决方案3】:

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

我只是想把我的两分钱花在一个针对用户问题的更短且非常具体的问题上。

/* 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;
    }
}

【讨论】:

猜你喜欢
  • 2021-09-16
  • 2020-11-28
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
相关资源
最近更新 更多