当前位置: 首页 > 知识库问答 >
问题:

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

洪高阳
2023-03-14

我想使用PHP从JSON格式的文件中提取键值对输出,并将其放入html表中

正如我们在那篇文章中所讨论的,将问题分开,以避免同一个问题变得杂乱无章。

{"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个答案

方昊
2023-03-14

在你的例子中,你可以使用explodeinfradephp函数来获得你想要的输出。我添加了几行代码:

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

?>
柯栋
2023-03-14

恐怕问题出在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来读取文件

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

和丰羽
2023-03-14

你的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>";

那就这样吧

 类似资料:
  • 目前我有以下输入json。我想提取值为attrs.name = Details的对象,并将其附加到输出json中(在attrs数组之外)。目前,虽然我能够将它附加到输出JSON中,但我仍然在attrs中得到该对象的副本。我想要这个副本被删除。 输入 JSON : 属性。name=“详细信息”可以在attrs中按任意顺序排列 输出量的希望值 电流输出 使用的颠簸规格 有没有办法删除仍在 attrs

  • 我正在研究posenet的tensorflow实现,以便实时进行姿态估计,如果可能的话,也可以在离线模式下进行姿态估计。我正在调查下列回购: https://github.com/tensorflow/tfjs-models/tree/master/posenet 在以下代码部分的以下函数中读出关键点 https://github.com/tensorflow/tfjs-models/blob/m

  • 我正在构建一个PHP应用程序。我需要做一个ajax调用一个PHP脚本,重新调整超文本标记语言代码。但是,我还需要接收一个变量以及PHP输出。我试图用JSON实现这一点,但是我得到了PHP输出或只是JSON数据。当我使用dataType:'json'在我的ajax调用: 我无法从PHP脚本中的“echo”获取输出: 因此,在jQueryAjax调用中,我需要接收作为PHP输出的“HelloWorld

  • 本文向大家介绍php读取csv文件并输出的方法,包括了php读取csv文件并输出的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php读取csv文件并输出的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的php程序设计有所帮助。

  • 问题内容: 我正在尝试使用PHP从以下JSON文件中获取数据。我特别想要“ temperatureMin”和“ temperatureMax”。 这可能真的很简单,但是我不知道该怎么做。我被困在file_get_contents(“ file.json”)之后该做什么。一些帮助将不胜感激! 问题答案: 使用以下命令获取JSON文件的内容: 现在使用解码JSON : 您有一个包含所有信息的关联数组。

  • 我在使用SOAP向服务器提交用户数据时遇到了一些麻烦。我得到的只是: 获取http正文时出错,没有内容长度,连接关闭或分块数据,我做错了吗? 最后一个响应头:http/1.1 200 OK x-站点授权:jenppb601内容-位置:http://.***.*/general/html/pages/layouts/columncontent.jsp内容-语言:de-DE内容-类型:text/htm