我有一个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()出错。
答案 0 :(得分:0)
file_get_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 />';
}
}
}
整天都很忙,但刚才再看一遍,并认为使用array_walk_recursive
会更简单。
$json=json_decode( file_get_contents('summary.json'), true );
if( json_last_error() !== JSON_ERROR_NONE ) exit('Invalid JSON data');
function arrwalker($item,$key){/* simple callback */
echo "<div><span class='bold'>$key</span> $item</div>";
}
array_walk_recursive( $json, 'arrwalker' );
答案 1 :(得分:0)
尝试这样的事情:
$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_dump
查看$data
对象在被json_decode()
解码后的样子。
这将逐行读取文件,每行单独json_decode
。您提供的代码段不完整或有效json
。