使用PHP从JSON文件中提取键值对输出 [英] Extract key value pair output from JSON file using PHP

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

问题描述

我想使用PHP从JSON格式的文件中提取键值对输出,并将其放入html表&具有精确键列的数据库.我尝试了

I would like to extract key value pair output from JSON formatted file using PHP and put into html table & database with exact key column. I tried code mentioned in Extract JSON ouput to get line by line key pair values using PHP but it doesn't work for multiple lines and gives wrong output from 2nd line onwards itself due to multiple lines from 2nd key.

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

As we discussed in that, filing separate question to avoid clutter the same question.

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

推荐答案

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

Your JSON seems not be formatted well in first place. You missed commas and square brackets.

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

This is a very basic solution but you can correct your JSON following this method:

添加逗号

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

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

Clean a bit the code (this is rough. Good for your case but not the best at all!)

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

就这样

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

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