0

how do you get a specific values inside a returned JSON from a twitter API and placed it on a HTML/CSS markup?

for example i have this returned from API

{"id":24876424,
 " id_str":"24876424",
 "name":"james bond",
 "screen_name":"jbond",
 "location":"tokyo",
 "description":"this is my tweet"
} ...

i only want to get the name and description part and put it inside a <div>.

<div class="twett">
    <div id="name"> "name" </div>
    <div id="desc"> "description </div>
</div>

and this is my PHP code in case you need to see it

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=jbond&count=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();
?>
bobbyjones
  • 2,029
  • 9
  • 28
  • 47
  • Asked on Stack Overflow at least a BILLION times. Search for "Retrieving data from JSON", please! – DevlshOne Sep 14 '13 at 07:03
  • possible duplicate of [How to list the properties of a javascript object](http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) – DevlshOne Sep 14 '13 at 07:04

2 Answers2

0

The response you are getting is JSON.

You used to be able to get response in XML, RSS and more, but now the only response type you will receive is JSON.

PHP has multiple methods for dealing with JSON. To encode data, you would use json_encode(). However, you want to decode it, so you want to be doing the following:

print_r(json_decode($yourData));

You can iterate around this data using a foreach() loop.

Remember, assuming you json_decode() your results into $yourData:

To access array properties, use: $yourData['propertyName']
To access object properties, use: $yourData->propertyName
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
0

You need to make the json request readable

$twitter = json_decode($twitter);

Within your HTML

<div class="twett">
    <div id="name"> <?php echo $twitter["name"]; ?> </div>
    <div id="desc"> <?php echo $twitter["description"]; ?> </div>
</div>

if There are multiple records returned you would need to do this

<?php
foreach(twitter as $tweet) {
   echo "<div class='twett'>";
   echo "<div id='name'>".$tweet["name"]."</div>";
   echo "<div id='desc'>".$tweet["description"]."</div>";
   echo "</div>";
}
?>
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37