【问题标题】:JSON DECODE using PHP使用 PHP 进行 JSON 解码
【发布时间】:2016-08-11 20:37:18
【问题描述】:

我正在做一些 JSON 解码 - 我按照本教程进行了很好的解释 - How To Parse JSON With PHP

还有我用的PHP代码

 <?php
 $string='{"person":[
            {
                "name":{"first":"John","last":"Adams"},
                "age":"40"
            },
            {
                "name":{"first":"Thomas","last":"Jefferson"},
                "age":"35"
            }
         ]}';

$json_a=json_decode($string,true);

$json_o=json_decode($string);


// array method
foreach($json_a[person] as $p)
{
echo '

Name: '.$p[name][first].' '.$p[name][last].'

Age: '.$p[age].'

';

}




// object method
foreach($json_o->person as $p)
{
echo '

<br/> Name: '.$p->name->first.' '.$p->name->last.'

Age: '.$p->age.'

';
}


 ?>

它工作正常...但我担心我只需要 Thomas 的姓氏和年龄的详细信息。我需要处理这个以仅提取某些特征,而不是所有对象。

【问题讨论】:

  • 这与 JSON 无关,也与 decode 无关。这是检索 PHP 数据结构的一部分的问题。另外,不要将变量命名为 json_ajson_o,因为它们不是 JSON。您的 $string 是唯一的 JSON 字符串。
  • 你的问题真的是这样吗:我为什么不在循环中添加if ($p-&gt;name-&gt;first === 'Thomas') {
  • 谢谢。我有一个 JSON 文件 - [raw.githubusercontent.com/mledoze/countries/master/… - 我只需要检索一个国家的货币.. 不完全.. 你说我需要使用'if ()'
  • 是的,看我的回答。

标签: php json decode


【解决方案1】:

提供您提供链接的 JSON 数据,这应该返回给定国家/地区的货币值:

$country_data = json_decode(file_get_contents("https://raw.githubusercontent.com/mledoze/countries/master/countries.json"), TRUE);

function get_currency($name) {
    global $country_data;

    $name = strtolower($name);
    $output = reset(array_filter($country_data, function ($value, $key) use($name) {
        if(strtolower($value['name']['common']) === $name || strtolower($value['name']['official']) === $name) {
            return true;
        }
    }, ARRAY_FILTER_USE_BOTH))['currency'];
    return ($output) ? $output : array();
}

/* Return same results */

echo "<pre>";
print_r(get_currency("Islamic Republic of Afghanistan"));
echo "</pre>";

echo "<pre>";
print_r(get_currency("Afghanistan"));
echo "</pre>";

注意:上述函数不区分大小写。如果您需要支持区分大小写,请删除 strtolower() 函数引用。

编辑:

  • 更正了 sn-p 中的一个错误。

编辑 2:

  • 如果找到国家名称,则返回货币数组;如果找不到国家,则返回空数组array()
  • 现在将根据common 名称和official 名称检查传递给get_currency() 的名称。传递任何一个都将返回一个值。

【讨论】:

    【解决方案2】:

    给定this JSON,可以得到一个国家的货币如下:

    function getCurrencyFor($arr, $findCountry) {
        foreach($arr as $country) {
            if ($country->name->common == $findCountry) {
                $currency = $country->currency[0];
                break;
            }
        }
        return $currency;
    }
    
    $json = file_get_contents("https://raw.githubusercontent.com/mledoze/countries/master/countries.json");
    $arr = json_decode($json);
    // Call our function to extract the currency for Angola:
    $currency = getCurrencyFor($arr, "Angola");
    
    echo "Angola has $currency as currency";
    

    【讨论】:

    • 谢谢你,@trincot。 '$currency = getCurrencyFor($arr, "阿富汗");'我把安哥拉换成了阿富汗。仍然显示相同..“AWG 作为货币”你认为我必须在某处更改代码吗..
    • 代码有错误。 if 当然应该有== 而不是=。我真傻。
    • 我试图获取 function getCurrencyFor($arr, $findCountry) // 获取货币的函数 { foreach (array($currency, $capital) as &$country) { if ($country-> name->common == $findCountry) { 数组 $currency = $country->currency[0]; $资本= $国家->资本;休息; } } 返回 $ 货币; } $json = file_get_contents("raw.githubusercontent.com/mledoze/countries/master/…); $arr = json_decode($json); // 调用我们的函数来提取安哥拉的货币: $currency = getCurrencyFor($arr, "Aruba");
    • 对不起,我不明白你在说什么。
    • 我使用foreach (array($currency, $capital) as &amp;$country) { 来获取货币和资本两个对象的值,由于某些原因代码不起作用。这是沙盒sandbox.onlinephpfunctions.com/code/…谢谢
    【解决方案3】:

    实际上托马斯是名字而不是姓氏 试试这个代码..

    print "names:", ",".join(x["last"] for x in obj if x["first"] == "Thomas")

    【讨论】:

    • 现在再试一次,只要放在这里 join(x["last"] 你想要什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2021-02-28
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多