doudou3935 2016-05-05 16:12
浏览 94
已采纳

如何在PHP中处理复杂的json对象

I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). I've been able to call the service, retrieve the json body, and use json_decode as follows to create an array:

$insightList = json_decode($guzzleResponse ->getBody(), true);

However, I can't figure out how to get access to the field I'm interested in. I've searched for solution on the web and tried a few approaches that looked promising, but when I tried to integrate them, I couldn't get them to work. I'm a bit of a PHP novice, so if something doesn't work I'm not sure how to proceed.

The json structure is quite complex, with three top-level arrays - search, tweets, and next. I'm interested in the second of these, tweets. It's a pretty complex array - there is one entry per tweet. The field I want right now is tweets.cde.message.body. You can find the full schema here: https://cdeservice.eu-gb.mybluemix.net/rest-api/#!/messages/getTweets

This is the code I have so far:

 foreach($insightList as $cde) {
    foreach($cde as $message) {
    $insight = $message['body'];
    if(strlen($insight) > 60) {
      $posts[] = array(
      'id' => 99999999,
      //Temp; remove links from the text
      'text' => $insight,
      'category' => $insightCategory,
      'image' => 'false'
    );        
  }
 }
}

Here's a print_r of $insightList:

    [search] => Array (
    [results] => 28
    [current] => 28
    )
[tweets] => Array (
    [0] => Array (
        [cde] => Array (
            [author] => Array (
                [gender] => male
                [parenthood] => Array (
                    [isParent] => unknown
                    )
                [location] => Array (
                    [country] => 
                    )
                [maritalStatus] => Array (
                    [isMarried] => unknown
                    )
                )
            [content] => Array (
                [sentiment] => Array (
                    [evidence] => Array ( )
                    [polarity] => NEUTRAL
                    )
                )
            )
        [message] => Array (
            [postedTime] => 2015-01-13T09:42:16.000Z
            [verb] => share
            [link] => http://twitter.com/zWDOM/statuses/554936456477933569
            [generator] => Array (
                [displayName] => Twitter Web Client
                [link] => http://twitter.com
                )
            [body] => RT @VisualSuccess: "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com  @zWDOM @Rocket @JohnKnutson_IBM
            [favoritesCount] => 0
            [objectType] => activity
            [actor] => Array (
                [summary] => Seit über 25 Jahren im Mainframebereich, derzeit als Senior Consultant und IT Architekt für zEnterprise und Projektmanager beim IBM BP Cancom in Köln
                [image] => https://pbs.twimg.com/profile_images/424202233463308288/XQquUcnh_normal.jpeg
                [statusesCount] => 2309
                [utcOffset] => 3600
                [languages] => Array (
                    [0] => de
                    )
                [preferredUsername] => zWDOM
                [displayName] => Willi Domroese
                [postedTime] => 2009-12-17T01:39:25.000Z
                [link] => http://www.twitter.com/zWDOM
                [verified] => 
                )
            [provider] => Array (
                [displayName] => Twitter
                [link] => http://www.twitter.com
                [objectType] => service
                )
            [twitter_filter_level] => medium
            [twitter_entities] => Array (
                [urls] => Array (
                    [0] => Array (
                        [display_url] => ln.is/paper.li/visua…
                        [indices] => Array (
                            [0] => 77
                            [1] => 99
                            )
                        [expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
                        [url] => http://www.twitter.com
                        )
                    )
                [hashtags] =>  Array ( )
                [user_mentions] => Array (
                    [0] => Array (
                        [indices] => Array (
                            [0] => 3
                            [1] => 17
                            )
                        [screen_name] => VisualSuccess
                        [id_str] => 213337792
                        [name] => Predrag Gasic
                        [id] => 213337792
                        )
                    [1] => Array (
                        [indices] => Array (
                            [0] => 101
                            [1] => 107
                            )
                        [screen_name] => zWDOM
                        [id_str] => 97334013
                        [name] => Willi Domroese
                        [id] => 97334013
                        )
                    [2] => Array (
                        [indices] => Array (
                            [0] => 108
                            [1] => 115
                            )
                        [screen_name] => Rocket
                        [id_str] => 870584947
                        [name] => Rocket Software
                        [id] => 870584947
                        )
                    [3] => Array (
                        [indices] => Array (
                            [0] => 116
                            [1] => 132
                            )
                        [screen_name] => JohnKnutson_IBM
                        [id_str] => 16452310
                        [name] => John Knutson
                        [id] => 16452310
                        )
                    )
                [trends] =>  Array ( )
                [symbols] =>  Array ( )
                )
            [twitter_lang] => de
            [id] => tag:search.twitter.com,2005:554936456477933569
            [retweetCount] => 1
            [gnip] => Array (
                [urls] => Array (
                    [0] => Array (
                        [expanded_url] => http://linkis.com/paper.li/visualsucce/7zYNk
                        [expanded_status] => 200
                        [url] => http://www.twitter.com
                        )
                    )
                [language] => Array (
                    [value] => de
                    )
                )
            [object] => Array (
                [postedTime] => 2015-01-13T08:04:48.000Z
                [verb] => post
                [link] => http://twitter.com/VisualSuccess/statuses/554911928527888384
                [generator] => Array (
                    [displayName] => Linkis.com
                    [link] => http://linkis.com
                    )
                [body] => "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com ://www.twitter.com
                [objectType] => activity
                [actor] => Array (
                    [summary] => Ù† (N), Wirtschaftsinformatiker | SAP Consultant Logistics #bigdata #appdevelopment #webdesign #eCommerce #SocialMedia #contentmarketing #SmartHome #Journal
                    [image] => https://pbs.twimg.com/profile_images/2841607223/959b0d23646b1f24bd7b70deac160e2f_normal.jpeg
                    [statusesCount] => 14185
                    [utcOffset] => 3600
                    [languages] => Array (
                        [0] => de
                        )
                    [preferredUsername] => VisualSuccess
                    [displayName] => Predrag Gasic
                    [postedTime] => 2010-11-08T17:19:27.000Z
                    [link] => http://www.twitter.com/VisualSuccess
                    [verified] => 
                    )
                [provider] => Array (
                    [displayName] => Twitter
                    [link] => http://www.twitter.com
                    [objectType] => service
                    )
                [twitter_filter_level] => low
                [twitter_entities] => Array (
                    [urls] => Array (
                        [0] => Array (
                            [display_url] => ln.is/paper.li/visua…
                            [indices] => Array (
                                [0] => 58
                                [1] => 80
                                )
                            [expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
                            [url] => http://www.twitter.com
                            )
                        )
                    [hashtags] =>  Array ( )
                    [user_mentions] => Array (
                        [0] => Array (
                            [indices] => Array (
                                [0] => 82
                                [1] => 88
                                )
                            [screen_name] => zWDOM
                            [id_str] => 97334013
                            [name] => Willi Domroese
                            [id] => 97334013
                            )
                        [1] => Array (
                            [indices] => Array (
                                [0] => 89
                                [1] => 96
                                )
                            [screen_name] => Rocket
                            [id_str] => 870584947
                            [name] => Rocket Software
                            [id] => 870584947
                            )
                        [2] => Array (
                            [indices] => Array (
                                [0] => 97
                                [1] => 113
                                )
                            [screen_name] => JohnKnutson_IBM
                            [id_str] => 16452310
                            [name] => John Knutson
                            [id] => 16452310
                            )
                        )
                    [trends] =>  Array ( )
                    [symbols] =>  Array (
                    )
                )
            )
        )
    )

Answers to this question would be greatly appreciated

  • 写回答

2条回答 默认 最新

  • douchen9569 2016-05-05 16:24
    关注

    Iterating $insightList will not get you to the cde level. And you don't actually want the cde; you want the message.

    Just iterate the tweets. You don't need a foreach to get keyed info from a PHP array.

    foreach($insightList['tweets'] as $tweet) {
            $insight = $tweet['message']['body'];
            if(strlen($insight) > 60) {
                $posts[] = array(
                'id' => 99999999,
                //Temp; remove links from the text
                'text' => $insight,
                'category' => $insightCategory,
                'image' => 'false'
            );        
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决