473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to access data inside Twitter json object

1 New Member
I am working with the Twitter API and finding it great fun and generally easy to use.

But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the object data with a foreach loop and print it out. But I want to do more with the data than just print it.

Just to be clear, I want to work with the daily and weekly trends but I am using the simpler current trends for this example. What I want to do is get the data out so that I can sort it, count duplicates, and remove duplicates.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. $json = file_get_contents("http://search.twitter.com/trends/current.json",0,null,null);
  2. $json = json_decode($json);
  3. $data = array();
  4. foreach ( $json->trends as $trends )
  5. {
  6.     foreach ($trends as $val)
  7.  
  8.         echo "<a href='http://search.twitter.com/search?q=" . urlencode("{$val->query}") . "'>{$val->name}</a> &nbsp; ";
  9.         //array_push($data, "{$val->query}","{$val->name}");
  10.  
  11.         $data["name"] = "{$val->name}";
  12.         $data["query"] = "{$val->query}";
  13. }
  14.  
The echo statement works perfectly. The array in the assignment statements ends up with only the final two key-value pairs. I also tried an array_push (which is commented out) to assign the values, with the same results.

The data I want from the json object is fairly deep. All I want to get at is the [name] and [query] values. Here is the output of a print_r for the original $json object:

Expand|Select|Wrap|Line Numbers
  1. stdClass Object
  2. (
  3.     [trends] => stdClass Object
  4.         (
  5.             [2009-12-03 21:10:18] => Array
  6.                 (
  7.                     [0] => stdClass Object
  8.                         (
  9.                             [name] => #whoiam
  10.                             [query] => #whoiam
  11.                         )
  12.  
  13.                     [1] => stdClass Object
  14.                         (
  15.                             [name] => #thingsilove
  16.                             [query] => #thingsilove
  17.                         )
  18.  
  19.                     [2] => stdClass Object
  20.                         (
  21.                             [name] => Tiger Woods
  22.                             [query] => "Tiger Woods"
  23.                         )
  24.  
  25.                     [3] => stdClass Object
  26.                         (
  27.                             [name] => Pleasure P
  28.                             [query] => "Pleasure P"
  29.                         )
  30.  
  31.                     [4] => stdClass Object
  32.                         (
  33.                             [name] => Christmas
  34.                             [query] => Christmas
  35.                         )
  36.  
  37.                     [5] => stdClass Object
  38.                         (
  39.                             [name] => New Moon
  40.                             [query] => "New Moon"
  41.                         )
  42.  
  43.                     [6] => stdClass Object
  44.                         (
  45.                             [name] => #seenewmoonagain
  46.                             [query] => #seenewmoonagain
  47.                         )
  48.  
  49.                     [7] => stdClass Object
  50.                         (
  51.                             [name] => Grammy
  52.                             [query] => Grammy
  53.                         )
  54.  
  55.                     [8] => stdClass Object
  56.                         (
  57.                             [name] => Holiday
  58.                             [query] => Holiday
  59.                         )
  60.  
  61.                     [9] => stdClass Object
  62.                         (
  63.                             [name] => Santa
  64.                             [query] => Santa
  65.                         )
  66.  
  67.                 )
  68.  
  69.         )
  70.  
  71.     [as_of] => 1259874618
  72. )
  73.  
What am I missing here?
Thanks for the help,
Jon Meek
Dec 3 '09 #1
0 2486

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6749
by: Andy Fish | last post by:
Hi, I have a webpage with some javascript that constructs some data structures, some of which contain nested structures or arrays of other structures. It's the same order of complexity as a web.xml file, say. I need to post the whole lot to the server and I'm not sure which is the best way to do it. It has to be browser-independent. The only ways I can think of are:
2
1687
by: Pavils Jurjans | last post by:
Hello, I have a fairly complex project with server-side written in C# (.NET), and client-side heavily relying on the presence on JavaScript-compatible scripting engine. One of the features thie project utilizes is "virtual POST", ie, client side submits the data to the server side, using Microsoft.XMLHTTP ActiveX Object (in MSIE), or XMLHttpRequest class in Mozilla, and when the server returns reply, processes it in client side to run...
20
6848
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object than its XML representation (so of course may argue).
7
2281
by: Noozer | last post by:
I've been doing some reading about "AJAX" and it sounds pretty interesting. While I can make it work, it seem like I'm doing much of it the "hard way". I was wondering what kind of code that others use to pass data via XMLHTTPRequest. My code is usually in ASP, with VBScript or Javascript for the client side of things. I've done a bit of PHP as well. Definately not an expert by any means, but I do understand programming - I just don't...
2
3744
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was considered bad practice because it can disrupt the use of for in loops on Objects. Am I incorrect? Thanks,
1
1723
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple JSON extension that allows cross-references, and I'm asking for your thoughts on a simple type system. I've dubbed this variation RJSON (Red's JSON) for lack of a shorter name. Here's how it works right now:
2
3313
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
6
2391
by: Jasper | last post by:
Hi, Maybe this is off-topic, but perhaps you can help. I'm looking for ideas on how to parse a data file. I dont know XML but I know it parses data in text format. I have a structured data file of the general form shown below. I dont have any definition of the data. Basically it looks like it is hierarchical, token/data pairs defined by brackets and square brackets. I would like to parse this out into some sort of data object(s) in...
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.