-5

Pls how do I retrieve the title from this json usng PHP

{
  "kind": "plus#activityFeed",
  "etag": "\"U2cYozG4eBioYd-8MEmyk8vsux4/98RQufcXcSAhnognM4kqOBjWygU\"",

  "items": [
  {
     "kind": "plus#activity",
      "etag": "\"U2cYozG4eBioYd-8MEmyk8vsux4/AMRVwIlEV6SoJ8ZkMErQKx04FIc\"",
      "title": "Reshared post from Jesse Oguntimehin\n \n\nMy post for #GNigeria day 2 
Cyberomin
  • 453
  • 2
  • 8
  • 22
  • 2
    You [convert it to an array](http://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array) and [access the corresponding index](http://php.net/manual/en/language.types.array.php). – Felix Kling Mar 17 '12 at 14:19
  • Everything you need is in the two links I posted. You just have to put the pieces together. If you don't know how to use arrays, then you first have to learn about them, otherwise you won't get far in PHP. I don't see a reason to have such a question for every possible piece of JSON data out there. – Felix Kling Mar 17 '12 at 14:28
  • If I'd have read your comment asking for code sample before I gave my answer I wouldn't have given it. The way you ask it is a bit rude... – teuneboon Mar 17 '12 at 14:28

2 Answers2

2

Use json_decode. Since the items part is an array I presume there are more than 1 items. So you'll have to loop through those.

<?php
$data = json_decode('the json here', true);
foreach ($data['items'] as $item) {
    echo $item['title'];
}
?>
teuneboon
  • 4,034
  • 5
  • 23
  • 28
1

The JSON is incomplete so my answer can only be to the snippet that you posted.

The syntax to reach the title field should be -

data.items[0].title

Note that I put items[0] because items is an array - notated with the square brakets [ ]. But the first element of that array seems to be an object - so we can continue with dot notation to reach the title property.

Convert it into a PHP array using json_decode($str,true) and take a look at the contents - that should be easier to get at.

Lix
  • 47,311
  • 12
  • 103
  • 131