代码之家  ›  专栏  ›  技术社区  ›  Jitesh Sojitra

使用PHP提取JSON输出以获取逐行密钥对值

  •  0
  • Jitesh Sojitra  · 技术社区  · 6 年前

    试用代码:

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

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

    键、状态、组件、currentVersion、expectedVersion、customerInfo的逐行值。

    Output

    3 回复  |  直到 5 年前
        1
  •  3
  •   Dexter0015    6 年前

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

    我调整了这里的功能: 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>";
            }
        }
    }
    
        2
  •  1
  •   A l w a y s S u n n y    6 年前

    怎么样 $someArray = json_decode($line,true); ? 因为没有第二个参数 true , json_decode() 将返回结果作为 object 在这种情况下,你必须使用 $value->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
  •  1
  •   Jim_M    6 年前

    我只是觉得我应该把我的两分钱投入到一个针对用户问题的较短且非常具体的问题中。

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