代码之家  ›  专栏  ›  技术社区  ›  Mark Henry

用JSON解码解析PHP中的JSON对象

  •  38
  • Mark Henry  · 技术社区  · 14 年前

    我试图从提供数据的web服务请求天气 JSON 格式。我的PHP请求代码没有成功是:

    $url="http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);
    echo $data[0]->weather->weatherIconUrl[0]->value;    
    

    这是返回的一些数据。为简洁起见,某些详细信息已被截断,但对象完整性仍保留:

    { "data": 
        { "current_condition": 
            [ { "cloudcover": "31",
                ... } ],  
          "request": 
            [ { "query": "Schruns, Austria",
                "type": "City" } ],
          "weather": 
            [ { "date": "2010-10-27",
                "precipMM": "0.0",
                "tempMaxC": "3",
                "tempMaxF": "38",
                "tempMinC": "-13",
                "tempMinF": "9",
                "weatherCode": "113",
                "weatherDesc": [ {"value": "Sunny" } ],
                "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
                "winddir16Point": "N",
                "winddirDegree": "356",
                "winddirection": "N",
                "windspeedKmph": "5",
                "windspeedMiles": "3" }, 
              { "date": "2010-10-28",
                ... },
    
              ... ]
            }
        }
    }
    
    6 回复  |  直到 8 年前
        1
  •  64
  •   Elzo Valugi    8 年前

    这似乎有效:

    $url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
    $content = file_get_contents($url);
    $json = json_decode($content, true);
    
    foreach($json['data']['weather'] as $item) {
        print $item['date'];
        print ' - ';
        print $item['weatherDesc'][0]['value'];
        print ' - ';
        print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
        print '<br>';
    }
    

    如果将json_decode的第二个参数设置为true,则会得到一个数组,因此不能使用->语法。我也建议您安装 JSONview Firefox extension

        2
  •  49
  •   user137621 user137621    14 年前

    如果改用以下方法:

    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);
    

        3
  •  23
  •   zod    14 年前

    试试这个例子

    $json = '{"foo-bar": 12345}';
    
    $obj = json_decode($json);
    print $obj->{'foo-bar'}; // 12345
    

    http://php.net/manual/en/function.json-decode.php

    注意-两个负数等于一个正数。:)

        4
  •  4
  •   mario    14 年前

    好像你忘了 [“值”] ->value :

    echo $data[0]->weather->weatherIconUrl[0]->value;
    
        5
  •  1
  •   cherankrish    6 年前

    当json解码时,强制它返回数组而不是对象。

    $data = json_decode($json, TRUE); -> // TRUE
    

    这将返回一个数组,您可以通过提供键来访问这些值。

        6
  •  0
  •   Mihai Iorga    12 年前

    您必须首先确保您的服务器允许远程连接,以便 file_get_contents($url)

        7
  •  0
  •   icedwater PedroMorgan    12 年前

    在编辑代码时(因为轻度强迫症),我注意到天气也是一个列表。你应该考虑一下

    echo $data[0]->weather[0]->weatherIconUrl[0]->value;
    

    以确保对正确的日期实例使用weathericonull。