`

Handling data in a PHP JSON Object

    博客分类:
  • php
阅读更多
Trends data from Twitter Search API in JSON.

Grabbing the file using:
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

How do I work with data from this object. As an array? Only really need to extract data from the [name] values.

JSON object contains:
stdClass Object
(
    [trends] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Vote
                    [url] => http://search.twitter.com/search?q=Vote
                )

            [1] => stdClass Object
                (
                    [name] => Halloween
                    [url] => http://search.twitter.com/search?q=Halloween
                )

            [2] => stdClass Object
                (
                    [name] => Starbucks
                    [url] => http://search.twitter.com/search?q=Starbucks
                )

            [3] => stdClass Object
                (
                    [name] => #flylady
                    [url] => http://search.twitter.com/search?q=%23flylady
                )

            [4] => stdClass Object
                (
                    [name] => #votereport
                    [url] => http://search.twitter.com/search?q=%23votereport
                )

            [5] => stdClass Object
                (
                    [name] => Election Day
                    [url] => http://search.twitter.com/search?q=%22Election+Day%22
                )

            [6] => stdClass Object
                (
                    [name] => #PubCon
                    [url] => http://search.twitter.com/search?q=%23PubCon
                )

            [7] => stdClass Object
                (
                    [name] => #defrag08
                    [url] => http://search.twitter.com/search?q=%23defrag08
                )

            [8] => stdClass Object
                (
                    [name] => Melbourne Cup
                    [url] => http://search.twitter.com/search?q=%22Melbourne+Cup%22
                )

            [9] => stdClass Object
                (
                    [name] => Cheney
                    [url] => http://search.twitter.com/search?q=Cheney
                )

        )

    [as_of] => Mon, 03 Nov 2008 21:49:36 +0000
)


<?php

$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

foreach ( $json_output->trends as $trend )
{
    echo "{$trend->name}\n";
}
分享到:
评论

相关推荐

    重庆全市JSONAPI

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于前后端数据传输。在前端开发中,JSONAPI 是一种规范,旨在使客户端和服务器之间的数据交换更加高效、一致。"重庆全市JSONAPI"可能指的是...

    json数据必导的夹包

    在Python编程中,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因其易于人阅读和编写,同时也易于机器解析和生成,被广泛应用于Web服务数据交互。当我们处理JSON数据时,通常需要导入Python标准...

    Ajax for Web Application Developers(Ajax网站开发)

    Creating a Utilities Object Handling Status Codes with an HTTP Object Part III: Creating Reusable Components Chapter 10. Accordion Getting Started Creating the Accordion Object Panel ...

    Ajax:handling different server data format 英文教程

    随着Web应用的不断发展,除了最初的XML格式外,JSON(JavaScript Object Notation)和简单的字符串格式也变得越来越常见。本文将详细介绍如何使用Ajax处理XML、JSON及字符串格式的数据。 #### 处理XML数据 **XML...

    json解析器源码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于Web应用程序之间传递数据。C++作为一款强大的编程语言,有许多库可以用来解析JSON,其中之一就是我们这里提到的开源JSON解析器。这个解析...

    C# Newtonsoft.Json类库源码包

    **C# Newtonsoft.Json类库源码包** Newtonsoft.Json是一个广泛使用的C# JSON库,由James Newton-King 创建,它提供了高效且功能丰富的JSON序列化和反序列化能力。这个类库是.NET开发者处理JSON数据时的重要工具,...

    Beginning PHP 5.3

    - **Consuming JSON/XML Data:** Techniques for consuming and processing JSON and XML data in PHP. - **Email Functionality:** Discussion on sending emails using PHP. **Chapter 17: Generating Images ...

    HTTP+JSON--ABAP交互方法.rar

    5. **GET_DATA_FROM_JSON 返回解析 (A).docx, GET_JSON_FORMAT 输入解析 (A).docx** - 这两部分分别涉及从JSON字符串获取数据到ABAP变量(GET_DATA_FROM_JSON),以及将ABAP数据结构转换为JSON格式(GET_JSON_FORMAT...

    Ajax-Data-Handling.pdf

    - **解析结果**:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。Ajax可以使用JSON.parse()函数来解析JSON字符串,将其转换为JavaScript对象。 - **...

    JSON API Document

    首先,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它是基于JavaScript的一个子集,但与语言无关。JSON API则是一种基于HTTP协议的RESTful接口...

    Strongly typed JSON library for Rust.zip

    在IT行业中,JSON(JavaScript Object Notation)是一种广泛使用的轻量级数据交换格式,它易于人阅读和编写,同时也易于机器解析和生成。在Rust编程语言中,处理JSON数据同样至关重要,特别是在网络服务、API交互...

    cjson.rar_JSON_cjson

    2. **数据结构(Data Structures)**:`cjson`使用自己的数据结构来表示JSON对象,包括` cJSON_Object `(表示JSON对象,类似于键值对的集合)、` cJSON_Array `(表示JSON数组,可以包含多个元素)、` cJSON_String...

    Professional JavaScript for Web Developers英文版

    fly graphicsJavaScript API changes in HTML5how browsers handle JavaScript errors and error handlingfeatures of JavaScript used to read and manipulate XML datathe JSON data format as an alternative to ...

    Python for Informatics

    6. **Text Processing**: Handling textual data is a common requirement in many fields. The book teaches string manipulation, regular expressions, and working with text files. 7. **Object-Oriented ...

    使用jsonrpc

    JSON-RPC(JavaScript Object Notation Remote Procedure Call)是一种轻量级的远程过程调用协议,主要应用于客户端与服务器之间的通信,特别是在Web服务中。它的设计灵感来源于XML-RPC,但采用了JSON作为数据交换...

    GWT in Action

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. This chapter explains how to leverage JSON to communicate with external APIs, including an example of using JSON to interact...

Global site tag (gtag.js) - Google Analytics