从JSON URL中提取数据


Extract Data from JSON URL

http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json是url,其中包含json数据。我想使用这些数据并向特定的手机发送短信,如果纬度和经度的值为no(从JSON中提取)。检查约束,我们可以通过php使用。但主要问题是如何从JSON文件中提取数据?

我不想给你解决方案,所以下面的内容应该足以让你开始。

  1. 使用file_get_contents读取JSON数据
  2. 使用json_decode将JSON字符串解析为PHP对象

你的代码应该是这样的:

$url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json";
$contents = file_get_contents($url);
$jsonObj = json_decode($contents);

你的意思是这样的吗?

<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
    echo "{$trend->name}'n";
}