0

有人可以帮我打印这个 json 解码数组中的“文本”值吗?我确实 echo $obj["text"] 得到了一个空白:(

啊,我做了 var_dump ;它说它是一个包含 1 个元素的数组中的 20 个元素的数组 :-) 所以在编辑了 42 次代码之后,我找到了解决方案,只是因为很棒的 Fangel 帮助打印文本需要放置以下行:echo $对象[0][“文本”];

$obj=[
        {
          "created_at":"Mon Sep 03 05:00:30 +0000 2012",
          "id":242487207418544128,
          "id_str":"242487207418544128",
          "text":"Clint, come to the Democratic Convention. We'll get you a coherent speech to read - and we'll even help you comb your hair.",
          "source":"web",
          "truncated":false,
          "in_reply_to_status_id":null,
          "in_reply_to_status_id_str":null,
          "in_reply_to_user_id":null,
          "in_reply_to_user_id_str":null,
          "in_reply_to_screen_name":null,
          "user":{
             "id":15376626,
             "id_str":"15376626",
             "name":"BarrackObama",
             "screen_name":"BarrackObama",
             "location":"Washington, D.C.",
             "url":null,
             "description":"President of the United States of America",
             "protected":false,
             "followers_count":94289,
             "friends_count":1,
             "listed_count":577,
             "created_at":"Thu Jul 10 12:05:37 +0000 2008",
             "favourites_count":0,
             "utc_offset":-18000,
             "time_zone":"Quito",
             "geo_enabled":false,
             "verified":false,
             "statuses_count":106,
             "lang":"en",
             "contributors_enabled":false,
             "is_translator":false,
             "profile_background_color":"E6EB6F",
             "profile_background_image_url":"http://a0.twimg.com/profile_background_images/76798997/PresidentialSeal.jpg",
             "profile_background_image_url_https":"https://si0.twimg.com/profile_background_images/76798997/PresidentialSeal.jpg",
             "profile_background_tile":false,
             "profile_image_url":"http://a0.twimg.com/profile_images/56441335/so_normal.jpg",
             "profile_image_url_https":"https://si0.twimg.com/profile_images/56441335/so_normal.jpg",
             "profile_link_color":"0FA7FF",
             "profile_sidebar_border_color":"EAFF08",
             "profile_sidebar_fill_color":"171CA6",
             "profile_text_color":"E69407",
             "profile_use_background_image":true,
             "default_profile":false,
             "default_profile_image":false,
             "following":null,
             "follow_request_sent":null,
             "notifications":null
          },
          "geo":null,
          "coordinates":null,
          "place":null,
          "contributors":null,
          "retweet_count":110,
          "entities":{
             "hashtags":[

             ],
             "urls":[

             ],
             "user_mentions":[

             ]
          },
          "favorited":false,
          "retweeted":false
       }
    ]
4

2 回答 2

0

这是打印它的方法:echo $obj[0]["text"];

于 2013-01-15T17:07:55.227 回答
0

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

协会

当 TRUE 时,返回的对象将被转换为关联数组。

当您将第二个参数设置为 true 时,您应该这样做echo $obj["text"];$obj->text;因为正如手册中所说,第二个参数为 true 时,强制 json_decode 返回关联数组,即使它是 JSON 中的对象。

据我所知,您的 JSON 中有一组对象。因此,在 json 解码之后,您还应该有一个对象数组。只需使用foreach循环遍历数组并打印所有项目的文本:

foreach($obj as $item) {
    echo $item->text + "<br/>";
}

或者像这样的关联数组(如果第二个参数json_decode为真):

foreach($obj as $item) {
    echo $item["text"] + "<br/>";
}
于 2013-01-10T09:02:48.593 回答