我正在通过API(zotero.org)检索书目数据,它类似于底部的样本(更复杂的方式 – 输入样本).
我想检索一个或多个记录并在页面上显示某些值.例如,我想循环遍历每个顶级记录并以精确格式化的引文打印数据.暂时忽略正确的围兜样式,假设我想为每个返回的记录打印出以下内容:
author1名称,author2名称,文章标题,出版物标题,密钥
这与代码不匹配,因为我显然错误地引用了键值对,并且只会弄乱它.
如果我请求JSON格式,下面的内容就像数据一样,尽管我可以请求XML数据.我不挑剔;我尝试过使用每一个都没有运气.
[ { "key": "123456","state": 100,"data": { "articleTitle": "Wombat coprogenetics: enumerating a common wombat population by microsatellite analysis of faecal DNA","authors": [ { "firstName": "Sam C.","lastName": "Smith" },{ "firstName": "Maxine P.","lastName": "Jones" } ],"pubTitle": "Australian Journal of Zoology","tags": [ { "tag": "scary" },{ "tag": "secret rulers of the world" } ] } },{ "key": "001122","data": { "articleTitle": "WOMBAT and WOMBAT-PK: Bioactivity Databases for Lead and Drug Discovery","authors": [ { "firstName": "Marius","lastName": "Damstra" } ],"pubTitle": "Chemical Biology: From Small Molecules to Systems Biology","tags": [ { "tag": "Wrong Wombat" } ] } } ]
如果括号,逗号等中有错误,这只是我的例子中的一个错字而不是我的问题的原因.
decode your json作为数组并迭代它作为流动的任何数组:
原文链接:https://www.f2er.com/php/135683.html$json_decoded= json_decode($json,true); $tab="\t"; foreach ($json_decoded as $key => $val) { echo "Article ".$val["key"]."\n" ; echo $tab."Authors :\n"; foreach ($val["data"]["authors"] as $key => $author){ echo $tab.$tab. ($key+1) ." - ".$author["firstName"]. " ".$author["lastName"]."\n"; } echo $tab."Article Title: ".$val["data"]["articleTitle"] ."\n"; echo $tab."Publication Title: ".$val["data"]["pubTitle"] ."\n"; echo $tab."Key: ".$val["key"]."\n"; }
并且您可以使用与流动相同的xml方法:
$xml = simplexml_load_string($xmlstring); $json = json_encode($xml); $json_decoded = json_decode($json,TRUE); //the rest is same
对于xml,您可以使用SimpleXml的功能
或DOMDocument班
小费
知道你转换为数组后返回给你的数据结构使用var_dump($your_decoded_json)进行调试