如何打印单个JSON元素

时间:2015-07-10 23:36:33

标签: php json

我是JSON的新手,但是我想弄明白,但是我发现了一个简单的打印错误。

我想打印得分是一个简单的开始。我一直在阅读有关JSON的内容,但我不知道我做错了什么。

错误消息:

PHP警告:在第11行的/testing.php中为foreach()提供的参数无效

 <?php


$string = file_get_contents('URL');

$json = json_decode($string);


foreach ($json['pos'] as $score)
  {
   echo "pos:". $score['score'] ."\n";
  };

print_r($json);

  ?>


  {
  "pos": [
   {
     "pos": "1",
     "name": "Sarah",
     "game": "a",
     "score": "-10",
     "examscore": "-5"
   },
   {
     "pos": "T2",
     "name": "Brian ",
     "game": "F*",
     "score": "-8",
     "examscore": "-3"
   },
    {
    "pos": "T2",
     "name": "Joe",
     "game": "F*",
     "score": "-8",
     "examscore": "-1"
   },
  {
     "pos": "WD"
  }
 ] 
  }

2 个答案:

答案 0 :(得分:2)

你的foreach循环应该是:

foreach ($json->pos as $score) {
   echo "pos: " .$score->score ."\n";
};

让我们先了解JSON。如果您打印print_r($string);,您将获得:

    stdClass Object
(
    [pos] => Array
        (
            [0] => stdClass Object
                (
                    [pos] => 1
                    [name] => Sarah
                    [game] => a
                    [score] => -10
                    [examscore] => -5
                )

            [1] => stdClass Object
                (
                    [pos] => T2
                    [name] => Brian 
                    [game] => F*
                    [score] => -8
                    [examscore] => -3
                )

            [2] => stdClass Object
                (
                    [pos] => T2
                    [name] => Joe
                    [game] => F*
                    [score] => -8
                    [examscore] => -1
                )

            [3] => stdClass Object
                (
                    [pos] => WD
                )

        )

)

请务必注意,默认情况下json_decode(string)会返回 OBJECT

在上面的示例中,$json是一个对象,[pos]是一个数组。每当您访问对象的成员时,都使用以下格式:$object->member

即使[pos]是一个数组,它仍然是对象$json的成员 如果你看一下上面的JSON打印,你会发现[pos]实际上是一个对象数组! $object->member公式适用于访问其每个成员(姓名,游戏,分数等)。

希望这有帮助。

答案 1 :(得分:0)

默认情况下,如果您没有将第二个参数指定为&#34; true&#34;,则json_decode不会返回数组而是返回StdClass对象。 http://php.net/manual/en/function.json-decode.php

因此,如果您想将json_decode结果用作数组

替换此

Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1478, in    full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1574, in make_response
rv = self.response_class(rv, headers=headers, status=status)
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 758, in __init__
self.status = status
File "/usr/local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 862, in _set_status
self._status = to_native(value)
File "/usr/local/lib/python2.7/site-packages/werkzeug/_compat.py", line 111, in to_native
return x.encode(charset, errors)
AttributeError: 'NotFound' object has no attribute 'encode'

通过这个

$json = json_decode($string);

编辑:

在您的foreach中,您尝试显示$ score [&#39;得分&#39;]但是您在json中的最后一项不包含键&#34;得分&#34;。

替换

$json = json_decode($string, true);

通过这个

echo "pos:". $score['score'] ."\n";

希望它会对你有所帮助。