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

使用PHP提取JSON输出以获取逐行密钥对值,php,json,Php,Json,我想使用PHP提取JSON输出,并尝试了下面的代码,但我得到的是单个字符的输出,而不是行值。我读过类似的文章,但没有得到密钥对值,也没有得到单个字符的输出。如果字段值不存在,则可以使用null。如何逐行获取各个键的输出 试用代码: 线路输出: JSON(结果文件内容): 预期产出: 键、状态、组件、currentVersion、expectedVersion、customerInfo的逐行值 实际产量: 那怎么样$someArray=json\u decode($line,true)?因为如果没

我想使用PHP提取JSON输出,并尝试了下面的代码,但我得到的是单个字符的输出,而不是行值。我读过类似的文章,但没有得到密钥对值,也没有得到单个字符的输出。如果字段值不存在,则可以使用null。如何逐行获取各个键的输出

试用代码: 线路输出: JSON(结果文件内容): 预期产出: 键、状态、组件、currentVersion、expectedVersion、customerInfo的逐行值

实际产量:

那怎么样
$someArray=json\u 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/>";
  }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
回声线;
$someArray=json_decode($line,true)#请参见此处的调整
foreach($someArray作为$key=>$value){
echo$value[“key”]。“,”$value[“status”]。“
”; } }
那怎么样
$someArray=json\u 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/>";
  }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
回声线;
$someArray=json_decode($line,true)#请参见此处的调整
foreach($someArray作为$key=>$value){
echo$value[“key”]。“,”$value[“status”]。“
”; } }
首先,@quitious\u mind正确地将json\u 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/>";
  }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
回声线;
$someArray=json_decode($line,true);
foreach($someArray作为$key=>$value){
回音键“,”$value.“
”; } }
但是,要小心,如果$value是一个数组,您将得到一个错误(不能只是回显一个数组),因此您需要反复处理json生成的数组

我调整了这里的功能: 并添加了一些测试来显示布尔值的字符串

它应该按照您的意愿显示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>";
        }
    }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
//回声线;
$someArray=json_decode($line,true);
递归写入($someArray);
}
函数RecursiveWrite($array){
foreach($key=>$value的数组){
echo$key.“,”;
if(是_数组($value)){
回声“
”; 递归写入($value); } elseif(is_bool($value)){ echo($value?'true':'false')。“
”; } 否则{ 回声$value.“
”; } } }
首先,@quitious\u mind正确地将json\u 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/>";
  }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
回声线;
$someArray=json_decode($line,true);
foreach($someArray作为$key=>$value){
回音键“,”$value.“
”; } }
但是,要小心,如果$value是一个数组,您将得到一个错误(不能只是回显一个数组),因此您需要反复处理json生成的数组

我调整了这里的功能: 并添加了一些测试来显示布尔值的字符串

它应该按照您的意愿显示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>";
        }
    }
}
while(!feof($resultFile)){
$line=fgets($resultFile);
//回声线;
$someArray=json_decode($line,true);
递归写入($someArray);
}
函数RecursiveWrite($array){
foreach($key=>$value的数组){
echo$key.“,”;
if(是_数组($value)){
回声“
”; 递归写入($value); } elseif(is_bool($value)){ echo($value?'true':'false')。“
”; } 否则{ 回声$value.“
”; } } }
首先,我同意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;
    }
}

首先,我同意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;
    }
}

感谢您的努力,但它是按照文章中添加的屏幕截图打印的,不幸的是,只有一个字符。感谢您的努力,但它是按照文章中添加的屏幕截图打印的,不幸的是,只有一个字符。我看到您有一行代码,上面写着“echo$line”结果在哪里?也添加了行输出。我看到您有一行代码,上面写着“echo$line”,结果在哪里?也添加了行输出。有关json_解码的更多信息,请访问此处:有关json_解码的更多信息,请访问此处:谢谢@Dexter0015,您的答案已被接受。但是,面临来自API的多行的问题。你想让我单独问一个问题来避免这里的混乱吗?你有什么建议吗?嗨@jitessojitra,是的。给你:谢谢@Dexter0015,你的答案被接受了。但是,面临来自API的多行的问题。你想让我单独问一个问题来避免这里的混乱吗?你有什么建议吗?嗨@jitessojitra,好的。给你: