使用PHP从JSON文件中提取键值对输出

时间:2019-02-05 10:30:11

标签: php json

我想使用PHP从JSON格式的文件中提取键值对输出,并将其放入具有精确键列的html表和数据库中。我尝试了Extract JSON ouput to get line by line key pair values using PHP中提到的代码,但是它不适用于多行,并且由于第二个键中有多行,所以第二行本身输出错误。

正如我们在那讨论过的那样,请提交单独的问题以避免混乱同一问题。

JSON data in multiple lines

JSON文件内容:

{"key":"SEM-5765","status":"Closed","components":"UX","affectedVersions":"SEM 8.8.x","fixVersions":"SurmaZuse-8.8.10","customerFacingInfo":"[https://goog.ezy.com/show_bug.cgi?id=109021 Bug 109021] - Content spoofing (text) via loginErrorCode \[CWE-345\]"} {"key":"SEM-3325","status":"Closed","components":"UX","affectedVersions":"SEM Prior to 8.7","fixVersions":"SurmaZuse-8.8.10","customerFacingInfo":"Fixed a number of bugs related to Delegated Admin in the Admin Console:
* \"New administrator\" creation button was not disabled for delegated admin without required rights ([https://goog.ezy.com/show_bug.cgi?id=108503 Bug 108503])
* \"Account Limits\" in domain settings could not be shown even when adminConsoleDomainLimitsTabRights was added ([https://goog.ezy.com/show_bug.cgi?id=108327 Bug 108327])
* Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://goog.ezy.com/show_bug.cgi?id=108499 Bug 108499])
* After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://goog.ezy.com/show_bug.cgi?id=108539 Bug 108539])"} {"key":"SEM-2755","status":"Closed","components":"UX","affectedVersions":"SEM Prior to 8.7","fixVersions":"SurmaZuse-8.8.10","customerFacingInfo":"Global Admin can now control the Downloads View (Admin > Tools > Download) and Help Center view for Delegated Admins."}

预期:

SEM-5765
Closed
UX
SEM 8.8.x
SurmaZuse-8.8.10
[https://goog.ezy.com/show_bug.cgi?id=109021 Bug 109021] - Content spoofing (text) via loginErrorCode \[CWE-345\]


SEM-3325
Closed
UX
SEM Prior to 8.7
SurmaZuse-8.8.10
Fixed a number of bugs related to Delegated Admin in the Admin Console: * \"New administrator\" creation button was not disabled for delegated admin without required rights ([https://goog.ezy.com/show_bug.cgi?id=108503 Bug 108503]) * \"Account Limits\" in domain settings could not be shown even when adminConsoleDomainLimitsTabRights was added ([https://goog.ezy.com/show_bug.cgi?id=108327 Bug 108327]) * Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://goog.ezy.com/show_bug.cgi?id=108499 Bug 108499]) * After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts ([https://goog.ezy.com/show_bug.cgi?id=108539 Bug 108539])

SEM-2755
Closed
UX
SEM Prior to 8.7
SurmaZuse-8.8.10
Global Admin can now control the Downloads View (Admin > Tools > Download) and Help Center view for Delegated Admins.

尝试输入的代码:

echo "<table class='table create-release-note-table'>
                <thead>
                    <tr><th>#</th><th>Ticket ID</th><th>Status</th><th>Components</th><th>Affected Versions</th><th>Fix Versions</th><th>Description</th></tr>
                </thead>
            <tbody>";
    $i = 0;

    $resultFile = fopen($resultURL, "r");
    #$lines = file($resultURL, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    #print_r ($lines);
    #exit;

    while (!feof($resultFile)) {
        $line = trim(fgets ($resultFile));
        $line = str_replace("\\\"", "", $line);
        $line = stripslashes($line);
        $lineArray = json_decode($line, true);
        echo "<tr><td>" . ++$i . "</td>";
        parseData($lineArray);
        echo "</tr>";
    }
    echo "</tbody></table>";
    fclose ($resultFile);


// Parse release note data

function parseData($array) {
   $value = str_replace(",", ";", $value);
   foreach ($array as $key => $value) {
     if (is_bool($value)) {
        echo ("<td>" . $value? 'true' : '') . "</td>";
     } else {
        echo "<td>" . $value . "</td>";
     }
  }
}

3 个答案:

答案 0 :(得分:3)

您的JSON似乎格式不正确。您错过了逗号和方括号。

这是一个非常基本的解决方案,但是您可以按照以下方法更正JSON:

添加逗号

$json = str_replace("} {", "}, {", $original_json);

清理一些代码(这很粗糙。适合您的情况,但根本不是最好的!)

$json = str_replace("\[", "[", $json);
$json = str_replace("\]", "]", $json);

将其包装在[]

$your_json_string = "[" . $json . "]";

然后您就可以使用

$json_parsed = json_decode($your_json_string);

echo "<table class='table create-release-note-table'>
<thead>
<tr>
<th>#</th>
<th>Ticket ID</th>
<th>Status</th>
<th>Components</th>
<th>Affected Versions</th>
<th>Fix Versions</th>
<th>Description</th>
</tr>
</thead>
<tbody>";

foreach($json_parsed as $json_object){
   echo "<tr>";
   echo "<td></td>";
   echo "<td>" . $json_object->key . "</td>";
   echo "<td>" . $json_object->status. "</td>";
   echo "<td>" . $json_object->components. "</td>";
   echo "<td>" . $json_object->affectedVersions. "</td>";
   echo "<td>" . $json_object->fixVersions . "</td>";
   echo "<td>" . $json_object->customerFacingInfo . "</td>";
   echo "</tr>";
}
echo "</tbody>
</table>";

就这样

答案 1 :(得分:2)

恐怕问题出在json文件方面。

如果我理解正确,那么您的问题中公开的JSON文件内容应该是单个json文件。

如果是这样,则json的格式似乎不正确。

您的json的结构如下(我删除了内容的某些部分以帮助阐明我的观点):

{"key":"SEM-5765"} 
{"key":"SEM-3325"} 
{"key":"SEM-2755"}

不是单个json,而是单个文件中的3个不同的json。

正确的json结构应该是:

[
{"key":"SEM-5765"}, 
{"key":"SEM-3325"}, 
{"key":"SEM-2755"},
]

哪个是json数组和正确的json结构。

所以我认为您有两种可能:

  1. 您可以更正json结构
  2. 您可以通过将每一行分开并将每行特征化为一个json来读取文件

无论哪种方式,您都必须在代码中添加一个步骤,以遍历每个line / json实体

答案 2 :(得分:1)

根据您的情况,您可以使用explodeimplode php函数来获取所需的输出。 我添加了几行代码:

$lineExplode = explode('}',$line);
$line = implode(',',$lineExplode);
$lineArray = json_decode("[".$line."]", true);

所有其他代码与您的示例相同:

<?php

echo "<table class='table create-release-note-table'>
                <thead>
                    <tr><th>#</th><th>Ticket ID</th><th>Status</th><th>Components</th><th>Affected Versions</th><th>Fix Versions</th><th>Description</th></tr>
                </thead>
            <tbody>";
    $i = 0;

    $resultFile = fopen($resultURL, "r");
    #$lines = file($resultURL, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    #print_r ($lines);
    #exit;

    while (!feof($resultFile)) {
        $line = trim(fgets ($resultFile));
        $line = str_replace("\\\"", "", $line);
        $line = stripslashes($line);
        $lineExplode = explode('}',$line);
        $line = implode(',',$lineExplode);
        $lineArray = json_decode("[".$line."]", true);
        echo "<tr><td>" . ++$i . "</td>";
        parseData($lineArray);
        echo "</tr>";
    }
    echo "</tbody></table>";
    fclose ($resultFile);


// Parse release note data

function parseData($array) {
   $value = str_replace(",", ";", $value);
   foreach ($array as $key => $value) {
     if (is_bool($value)) {
        echo ("<td>" . $value? 'true' : '') . "</td>";
     } else {
        echo "<td>" . $value . "</td>";
     }
  }
}

?>