Php 逐行检索JSON数据JSON文件

Php 逐行检索JSON数据JSON文件,php,json,Php,Json,我有一个JSON文件,每行有一个不同的JSON响应。我希望每行检索相同的数据(摘要) 我的文件结构如下: {"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"11246","self":"http://api-factory.orange-labs.fr:8080/rest/api/latest/issue/11246","key":"TK-44","fields":{"i

我有一个JSON文件,每行有一个不同的JSON响应。我希望每行检索相同的数据(摘要)

我的文件结构如下:

{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"11246","self":"http://api-factory.orange-labs.fr:8080/rest/api/latest/issue/11246","key":"TK-44","fields":{"issuetype":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/issuetype/3","id":"3","description":"A task that needs to be done.","iconUrl":"http://api-factory.orange-labs.fr:8080/images/icons/issuetypes/task.png","name":"Task","subtask":false},"timespent":null,"project":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/project/10300","id":"10300","key":"TK","name":"TEST KANBAN","avatarUrls":{"48x48":"http://api-factory.orange-labs.fr:8080/secure/projectavatar?avatarId=10011","24x24":"http://api-factory.orange-labs.fr:8080/secure/projectavatar?size=small&avatarId=10011","16x16":"http://api-factory.orange-labs.fr:8080/secure/projectavatar?size=xsmall&avatarId=10011","32x32":"http://api-factory.orange-labs.fr:8080/secure/projectavatar?size=medium&avatarId=10011"}},"fixVersions":[],"aggregatetimespent":null,"resolution":null,"resolutiondate":null,"workratio":-1,"lastViewed":null,"watches":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/issue/TK-44/watchers","watchCount":1,"isWatching":true},"created":"2016-08-17T11:33:16.000+0200","priority":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/priority/3","iconUrl":"http://api-factory.orange-labs.fr:8080/images/icons/priorities/medium.png","name":"Medium","id":"3"},"customfield_10100":null,"labels":[],"timeestimate":null,"aggregatetimeoriginalestimate":null,"versions":[],"issuelinks":[],"assignee":null,"updated":"2016-08-17T16:32:33.000+0200","status":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/status/10403","description":"This status is managed internally by JIRA Agile","iconUrl":"http://api-factory.orange-labs.fr:8080/images/icons/subtask.gif","name":"Opportunities","id":"10403","statusCategory":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}},"components":[],"timeoriginalestimate":null,"description":null,"customfield_10210":null,"customfield_10211":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/customFieldOption/10125","value":"API lead","id":"10125"},"customfield_10212":null,"timetracking":{},"customfield_10203":null,"customfield_10006":null,"customfield_10204":null,"customfield_10205":null,"customfield_10206":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/customFieldOption/10103","value":"B. Mignot","id":"10103"},"attachment":[],"customfield_10009":"0|i0018n:","customfield_10207":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/customFieldOption/10109","value":"P. Metton","id":"10109"},"aggregatetimeestimate":null,"customfield_10208":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/customFieldOption/10122","value":"not assigned","id":"10122"},"customfield_10209":null,"summary":"API Orange Wholesale FR","creator":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/user?username=AGRO","name":"AGRO","key":"agro","emailAddress":"rayan.dinar@orange.com","avatarUrls":{"48x48":"http://api-factory.orange-labs.fr:8080/secure/useravatar?avatarId=10122","24x24":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=small&avatarId=10122","16x16":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=xsmall&avatarId=10122","32x32":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=medium&avatarId=10122"},"displayName":"ROBERTO AGRO","active":true,"timeZone":"Europe/Paris"},"subtasks":[],"reporter":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/user?username=AGRO","name":"AGRO","key":"agro","emailAddress":"rayan.dinar@orange.com","avatarUrls":{"48x48":"http://api-factory.orange-labs.fr:8080/secure/useravatar?avatarId=10122","24x24":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=small&avatarId=10122","16x16":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=xsmall&avatarId=10122","32x32":"http://api-factory.orange-labs.fr:8080/secure/useravatar?size=medium&avatarId=10122"},"displayName":"ROBERTO AGRO","active":true,"timeZone":"Europe/Paris"},"customfield_10000":null,"aggregateprogress":{"progress":0,"total":0},"customfield_10001":null,"customfield_10200":null,"customfield_10201":null,"customfield_10004":null,"customfield_10202":null,"environment":null,"duedate":null,"progress":{"progress":0,"total":0},"comment":{"startAt":0,"maxResults":0,"total":0,"comments":[]},"votes":{"self":"http://api-factory.orange-labs.fr:8080/rest/api/2/issue/TK-44/votes","votes":0,"hasVoted":false},"worklog":{"startAt":0,"maxResults":20,"total":0,"worklogs":[]}}}
{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"11247","
我的代码:

$json = file_get_contents("summary.json");
foreach ($json as $lineNumber => $lineContent)
{
$parsed_json = json_decode($lineNumber);
$name = $parsed_json->{'fields'}->{'summary'};

echo $name;
}

这段代码中的foreach()有一个错误。

file\u get\u contents
返回一个字符串-因此您需要在尝试遍历它之前对其进行解码。一些包含的内容是数组——递归迭代器可能比下面的更好/更干净

$data=file_get_contents('summary.json');
$json=json_decode($data,true);
if( json_last_error() !== JSON_ERROR_NONE ) exit('Invalid JSON data');

foreach($json as $i => $o){
    $keys=array_keys( $o );
    foreach( $keys as $key ) {
        if( is_array( $o[$key] ) ){
            $a=$o[$key];
            foreach($a as $v)echo $v.'<br />';
        } else {
            echo $key.' -> '.$o[$key].'<br />';
        }
    }
}

file\u get\u contents
返回一个字符串,因此您需要在尝试遍历它之前对其进行解码。一些包含的内容是数组——递归迭代器可能比下面的更好/更干净

$data=file_get_contents('summary.json');
$json=json_decode($data,true);
if( json_last_error() !== JSON_ERROR_NONE ) exit('Invalid JSON data');

foreach($json as $i => $o){
    $keys=array_keys( $o );
    foreach( $keys as $key ) {
        if( is_array( $o[$key] ) ){
            $a=$o[$key];
            foreach($a as $v)echo $v.'<br />';
        } else {
            echo $key.' -> '.$o[$key].'<br />';
        }
    }
}

试着这样做:

$fileHandle = fopen("summary.json", "r");
if ($fileHandle) {
    while ($line = fgets($fileHandle) !== false) {
        $data = json_decode($line);
        $name = $data->summary;
        //var_dump($data);
    }

    fclose($fileHandle);
}
使用
var\u dump
查看
$data
对象被
json\u decode()解码后的样子


这将逐行读取文件,并分别对每行进行
json\u解码。您提供的代码段不完整或无效
json

试试这样的方法:

$fileHandle = fopen("summary.json", "r");
if ($fileHandle) {
    while ($line = fgets($fileHandle) !== false) {
        $data = json_decode($line);
        $name = $data->summary;
        //var_dump($data);
    }

    fclose($fileHandle);
}
使用
var\u dump
查看
$data
对象被
json\u decode()解码后的样子


这将逐行读取文件,并分别对每行进行
json\u解码。您提供的代码段不完整或无效
json

add
$json=json\u decode($json,true)
错误是“为foreach()提供的参数无效”,这是因为您无法在foreach中传递json,您需要先将json转换为数组,然后通过loopadd
$json=json\u decode($json,true)
错误是“为foreach()提供的参数无效”这是因为您无法在foreach中传递json,您需要先将json转换为数组,然后通过循环。感谢您的响应,但我有相同的错误“为foreach()提供的参数无效”问题中的数据过早终止-如果实际数据共享此错误,那么这将解释为什么未正确解析它。感谢您的回答,但我有相同的错误“为foreach()提供的参数无效”问题中的数据过早终止-如果实际数据共享此错误,那么这将解释为什么未正确解析它。