douxian4888 2013-09-23 15:56 采纳率: 0%
浏览 44
已采纳

访问JSON变量的奇怪响应

I have this code:

<?php
$jsonurl = "http://api.wipmania.com/json";
$jsonfgc = file_get_contents($jsonurl);
$json = json_decode($jsonfgc, true);

foreach ( $json["address"] as $address => $country_code )
    {   
      echo $address["country_code"];
    }
?>

However, when I print this out, I only get "cccccr". I try just echoing $country_code but I get "North AmericaNA-United StatesUS-". Any help?

  • 写回答

5条回答 默认 最新

  • doujiang7258 2013-09-23 16:01
    关注

    If you're actually trying to grab the country code, do the following:

    echo $json['address']['country_code'];
    

    A loop is not required there.


    I try just echoing $country_code but I get "North AmericaNA-United StatesUS-". Any help?

    For an explanation as to why this is happening:

    You probably didn't add any linebreaks in your code. So it appears as if it's a single string. It's actually not.

    Try:

    foreach ( $json["address"] as $address => $country_code )
    {   
        echo $country_code."<br/>
    ";
    }
    

    The output should be:

    North America
    NA
    -
    United States
    US
    -
    

    See the difference!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组