简体   繁体   English

使用PHP解析来自API的json响应

[英]parsing a json response from an API with PHP

I'm trying to parse a json response from an API but buy altering some existing PHP code I created however I'm having difficulty. 我正在尝试解析来自API的json响应但是购买改变我创建的一些现有PHP代码但是我遇到了困难。 This is the json response 这是json的回应

"response":{
  "status":"ok",
  "userTier":"free",
  "total":10,
  "startIndex":1,
  "pageSize":10,
  "currentPage":1,
  "pages":1,
  "results":[{
    "id":"lifeandstyle/series/cycling",
    "type":"series",
    "webTitle":"Cycling",
    "webUrl":"http://www.guardian.co.uk/lifeandstyle/series/cycling",
    "apiUrl":"http://content.guardianapis.com/lifeandstyle/series/cycling",
    "sectionId":"lifeandstyle",
    "sectionName":"Life and style"
  }

Trying to parse all the information within the "webTitle" and "webUrl" sections 试图解析“webTitle”和“webUrl”部分中的所有信息

<?php 
require_once 'Zend/Json.php'; 
$val = Zend_Json::decode($result); 
$arr = $val; 
if(preg_match_all("~<p>([\s\S]*?)</p>~i", $arr['parse']['text']['*'], $matches)){ 
if(is_array($matches[1])) foreach($matches[1] as $paragraph){ 
echo $paragraph;
}
}
?>

This code just parses content in p tags, how would I alter it? 这段代码只解析p标签中的内容,我该如何改变它?

Thanks 谢谢

I'm not 100% clear on your question. 我不是100%明确你的问题。 Zend_Json::decode uses json_decode under the hood, which is what @psion was referring to. Zend_Json :: decode使用了引擎盖下的json_decode,这就是@psion所指的内容。 I assume $result in your example 我假设你的例子中有$ result

$val = Zend_Json::decode($result);

holds the json you pasted beforehand. 握住你事先粘贴的json。

It looks to me like the json you posted is invalid or at least incomplete (because there is a missing ] and a missing } ). 它看起来像你发布的json无效或至少不完整(因为有一个缺失]和一个缺失} )。 I'm not sure what this has to do with parsing p tags, but anyway here's an example that parses the json and extracts the components you're interested in. It peels off the leading "response": bit from the json before decoding as well. 我不确定这与解析p标签有什么关系,但无论如何这里是一个解析json并提取你感兴趣的组件的例子。它解除了前面的“响应”:来自json的位,然后解码为好。

<?php
$sJson = '"response":{
  "status":"ok",
  "userTier":"free",
  "total":10,
  "startIndex":1,
  "pageSize":10,
  "currentPage":1,
  "pages":1,
  "results":[{
    "id":"lifeandstyle/series/cycling",
    "type":"series",
    "webTitle":"Cycling",
    "webUrl":"http://www.guardian.co.uk/lifeandstyle/series/cycling",
    "apiUrl":"http://content.guardianapis.com/lifeandstyle/series/cycling",
    "sectionId":"lifeandstyle",
    "sectionName":"Life and style"
  }]}';
$sJson = substr($sJson, strpos($sJson, ':') + 1);

// feel free to replace json_decode w/ Zend_Json::decode
$aNative   = json_decode($sJson);

$sWebTitle = $aNative->results[0]->webTitle;
$sWebUrl   = $aNative->results[0]->webUrl;

echo 'Web Title: ' . $sWebTitle . PHP_EOL;
echo 'Web URL  : ' . $sWebUrl . PHP_EOL;

Here's the output from the script 这是脚本的输出

Web Title: Cycling
Web URL  : http://www.guardian.co.uk/lifeandstyle/series/cycling

What do you mean by parse? 解析是什么意思? The JSON reply is ready to be used as soon as you decode it. 您可以在解码后立即使用JSON回复。 You don't have a $arr['parse']... you have $arr['webTitle'] which contains "Cycling". 你没有$arr['parse']...你有$arr['webTitle']其中包含“Cycling”。 What do you want to parse inside "Cycling"? 你想在“骑自行车”中解析什么?

Also, what is the deal with assigning the result to $val and then assigning $val to $arr? 另外,将结果分配给$ val然后将$ val分配给$ arr的处理是什么? Assign straight to $arr. 直接分配给$ arr。

Do print_r($val) and look at what you have. 做print_r($ val)并看看你有什么。 That will give you a better idea of what you need to do. 这样可以让您更好地了解自己需要做什么。 Most likely you are making things more complicated than they need to be. 很可能你的事情比他们需要的更复杂。

Tthere are json function within PHP that do not require calling Zend functions. PHP中的json函数不需要调用Zend函数。

http://us2.php.net/manual/en/book.json.php http://us2.php.net/manual/en/book.json.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM