【问题标题】:Issue with json parsing phpjson解析php的问题
【发布时间】:2014-04-03 09:38:33
【问题描述】:

我目前正在尝试使用 http://getpocket.com/developer/docs/v3/retrieve 路由将我的 php 应用程序绑定到 Pocket 应用程序 api。连接本身很好,我可以获得返回的 json。返回的json如下:

{
    "status": 1,
    "complete": 1,
    "list": {
        "446599840": {
            "item_id": "446599840",
            "resolved_id": "446599840",
            "given_url": "http://www.theguardian.com/media-network/media-network-blog/2013/jan/07/panasonic-changing-consumer-retail-technology",
            "given_title": "The changing consumer-retailer relationship: interview with Panasonic | Med",
            "favorite": "0",
            "status": "0",
            "time_added": "1395140204",
            "time_updated": "1395140204",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 6,
            "resolved_title": "Guardian Professional",
            "resolved_url": "http://www.theguardian.com/media-network/media-network-blog/2013/jan/07/panasonic-changing-consumer-retail-technology",
            "excerpt": "Time to give call centres a facelift? Businesses have an opportunity to reinvent their customer-service by applying new technologies in the right areas, says Simon Parkinson. Photograph: 13/Ocean/Corbis",
            "is_article": "0",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "0"
        },
        "482104798": {
            "item_id": "482104798",
            "resolved_id": "482104798",
            "given_url": "http://www.theintegratedretailer.com/news/795/retailers-urged-to-embrace-techno…o-create-knowledge-sharing-high-performing-and-highly-engaged-workforces/",
            "given_title": "Retailers urged to embrace technology to create knowledge sharing, high per",
            "favorite": "0",
            "status": "0",
            "time_added": "1395144267",
            "time_updated": "1395146556",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 3,
            "resolved_title": "Retailers urged to embrace technology to create knowledge sharing, high performing and highly engaged workforces",
            "resolved_url": "http://www.theintegratedretailer.com/news/795/retailers-urged-to-embrace-techno…o-create-knowledge-sharing-high-performing-and-highly-engaged-workforces/",
            "excerpt": "With evidence suggesting that businesses that achieve high levels of sustainable employee engagement average an operating margin close to three times higher than those with high traditional engagement, large retailers are being urged to explore the potential that new technology can play in improv",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "502"
        },
        "571816084": {
            "item_id": "571816084",
            "resolved_id": "571816084",
            "given_url": "http://www.hrreview.co.uk/supplier-news/supplier-training-development/british-sme-lights-the-fuse-for-free-online-education-anywhere-in-the-world/50353",
            "given_title": "British SME lights the Fuse for free online education anywhere in the world",
            "favorite": "0",
            "status": "0",
            "time_added": "1395144314",
            "time_updated": "1395144314",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 0,
            "resolved_title": "British SME lights the Fuse for free online education anywhere in the world",
            "resolved_url": "http://www.hrreview.co.uk/supplier-news/supplier-training-development/british-sme-lights-the-fuse-for-free-online-education-anywhere-in-the-world/50353",
            "excerpt": "A British SME, which has developed an online learning platform for some of the UK’s leading retailers, will demonstrate how commercial organisations implementing its technology are simultaneously supporting the social goal of delivering free secondary education content to any student anywhere in",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "429"
        },
        "571816595": {
            "item_id": "571816595",
            "resolved_id": "0",
            "given_url": "http://www.hrreview.co.uk/supplier-news/supplier-hr-strategy-practice/organisations-can-now-create-their-own-online-central-knowledge-bank/50085",
            "given_title": "Organisations can now create their own online central knowledge bank",
            "favorite": "0",
            "status": "0",
            "time_added": "1395144298",
            "time_updated": "1395144298",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 1
        },
        "571820367": {
            "item_id": "571820367",
            "resolved_id": "571820367",
            "given_url": "http://www.palife.co.uk/surveys-current-affairs/item/489-and-finally-something-for-the-boss",
            "given_title": "And finally... something for the boss",
            "favorite": "0",
            "status": "0",
            "time_added": "1395144281",
            "time_updated": "1395144281",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 2,
            "resolved_title": "And finally... something for the boss",
            "resolved_url": "http://www.palife.co.uk/surveys-current-affairs/item/489-and-finally-something-for-the-boss",
            "excerpt": "At PA Life we're devoted to bringing you the very latest news, tips, advice and recommendations on a whole smorgasbord of topics that we hope are of interest to you in your professional lives.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "310"
        },
        "571822317": {
            "item_id": "571822317",
            "resolved_id": "299897045",
            "given_url": "http://www.learningsolutionsmag.com/articles/1004/?utm_campaign=lsmag&utm_medium=some&utm_source=linkedin&goback=.gde_1821266_member_160113156",
            "given_title": "EMEA Reporter: African Virtual School is Model for World by Nic Laycock : L",
            "favorite": "0",
            "status": "0",
            "time_added": "1395140611",
            "time_updated": "1395140611",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 4,
            "resolved_title": "EMEA Reporter: African Virtual School is Model for World",
            "resolved_url": "http://www.learningsolutionsmag.com/articles/1004/",
            "excerpt": "What do you do when you have to serve out an employment restraint (a non-compete clause)? Go and found a social impact company, of course, using your income from your previous employment!",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "774"
        },
        "571823356": {
            "item_id": "571823356",
            "resolved_id": "0",
            "given_url": "http://uk.prweb.com/releases/2010/12/prweb4870124.htm",
            "given_title": "Fusion Universal Wins Coveted E-Learning Award Using Panviva's SupportPoint",
            "favorite": "0",
            "status": "0",
            "time_added": "1395140303",
            "time_updated": "1395140304",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 5
        }
    },
    "since": 1396516912
}

但是问题是我似乎无法有效地解析这个。整个数组似乎没有入口点(“列表”失败)。我认为每个数字条目也给我带来了问题。本质上,我试图遍历然后项目以提取相关数据,即每个条目的“given_title”,但无论哪种解析方法,我似乎都无法得到结果。

我目前在php中使用这种格式(明显错误):

<?php
foreach ($result['list'] as $item){
 echo "Item: ", $item['item_id'];
}
var_dump(json_decode($result,true));
?>

任何帮助将不胜感激。

非常感谢。

【问题讨论】:

  • $array = json_decode($result, true); foreach ($array['list'] as ...)...?!

标签: php json api parsing


【解决方案1】:

您首先需要将json数据转换为数组。

“true”参数确保它返回一个关联数组而不是一个对象。 pre标签用于格式化。

$result = json_decode($json, true);
echo "<pre>";
foreach ($result["list"] as $value) {
    print_r($value);
}
echo "</pre>";

【讨论】:

  • 嗨@Whitebird - 我已经尝试过使用 json 解码,我知道这就是我需要的,但是这样做会产生以下错误,也许我也应该在问题中显示这些错误:警告:'为 foreach()' 和 'uncaught TypeError: Cannot read property '446599840' of undefined' 提供的参数无效,这是我认为数值问题出现的地方。
  • 这是我用来测试的全部代码,你能告诉我区别吗? pastebin.com/05H2Lx76
  • 这很奇怪,好像我使用您的 pastebin 代码并在文件中包含 json 这似乎有效。但是,我正在通过经过身份验证的 Pocket api 请求提取此 json,当我切换回该 json 时,这似乎是它目前像以前一样失败的地方。我想我需要进一步看看等式的那一面,因为我可以让你的 pastebin 与 json 内联工作。根据您的原始答案,我将如何为每个打印项目 ID。
  • 没问题,我知道了 - print_r($value['item_id']);
【解决方案2】:

拳头,需要将json数据转为数组:

$result=json_decode($result);

foreach ($result['list'] as $item){
 echo "Item: ", $item['item_id'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多