Damien
Damien

Reputation: 333

Using a JSON feed with PHP

I Have a problem for using a json feed with php. For example :

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":4,
"twitter":6}
}],

[{"type":"article",
"article":[{
"title":"hello",
"number":{
"facebook":1,
"twitter":3}
}],

I have no problem to save the title :

$titre = $data[$i]['type'][0]['title'];

But i can't find how to save the facebook number. I have tried a lot of combinaison

$number = $data[0]['type'][$i]['scores']['facebook'][0];

OR

$number = $data[0]['type'][$i]['scores']['facebook'];

OR

$number = $data[0]['type'][$i]['scores']['facebook'][1];

No one works... Do you have an idea ? Many thanks guys !

Upvotes: 0

Views: 128

Answers (3)

Ioannis Lalopoulos
Ioannis Lalopoulos

Reputation: 1511

As others have stated the json you have supplied is not valid.

Looking at its structure it seems that the intent is to have an array of objects, each object having a "type" key and an embedded object with a key name that varies according to the value of the type key. (The varied type key (in both of the examples given the key is "article") holds a title, and an object which holds twitter and facebook counts.)

Two notes:

  1. from the broken json we are not sure if the embedded article object is an object or an array of objects so we will show the approach for both cases.
  2. probably the key name of the embedded object is variable and thus we need to deduce it every time from the type value (however in our examples it is always 'article'

Now to access that structure the following are valid when you know that the type is article (we suppose $data holds the decoded json):

$data[$i]['article']['title']

$data[$i]['article']['number']['facebook']

and if is embedded as an array of objects the above should be (to access the first object):

$data[$i]['article'][0]['title']

$data[$i]['article'][0]['number']['facebook']

However we probably need to deduce the key name because it seems that is variable so the general correct form is:

$typeName = $data[$i]['type']
$data[$i][$typeName]['title']
$data[$i][$typeName]['number']['facebook']

which in the expanded form for facebook is : $data[$i][$data[$i]['type']]['number']['facebook']

and if the case is that the 'articles' are embedded as an array of objects, then:

$typeName = $data[$i]
$data[$i][$typeName][0]['title']
$data[$i][$typeName][0]['number']['facebook']

which in the expanded form for facebook is: $data[$i][$data[$i]['type']][0]['number']['facebook']

Here is a script that runs against those two json structures, and access their data:

<?php
//Two possible jsons, it seems that:
//1)the embedded article object can be an object or an array of objects so we try two different json structures
//2)also it seems that the key of the embedded object is variable and thus we need to take it every time from the type value (however in our examples it is always 'article'
$json1 = '[{"type":"article", "article":{"title":"hello","number":{"facebook":4,"twitter":6}}},{"type":"article","article":{"title":"hello","number":{"facebook":1,"twitter":3}}}]';
$json2 = '[{"type":"article", "article":[{"title":"hello","number":{"facebook":4,"twitter":6}}]},{"type":"article","article":[{"title":"hello","number":{"facebook":1,"twitter":3}}]}]';
$data1 = json_decode($json1, true);
$data2 = json_decode($json2, true);

for ($i=0; $i<count($data1); $i++) {
    $articleType = $data1[$i]['type'];
    echo 'Title from element: ',$i, ' ', $data1[$i][$articleType]['title'],
    PHP_EOL;
    echo 'Facebook number from element ', $i, ' ',
    $data1[$i][$articleType]['number']['facebook'], PHP_EOL;
    echo 'Twitter number from element ', $i, ' ',
    $data1[$i][$articleType]['number']['twitter'], PHP_EOL;
}

//However if we have embedded an array of objects then we access this way (hardcoded to index 0)
$embIndex = 0;
for ($i=0; $i<count($data2); $i++) {
    $articleType = $data2[$i]['type'];
    echo 'Title from element ',$i, ' and embedded object index ',
    $embIndex, ': ',$data2[$i][$articleType][$embIndex]['title'], PHP_EOL;
    echo 'Facebook number from element ', $i, ' and embedded object index ',
    $embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['facebook'], PHP_EOL;
    echo 'Twitter number from element ', $i, ' and embedded object index ',
    $embIndex, ': ', $data2[$i][$articleType][$embIndex]['number']['twitter'], PHP_EOL;
}

and the output:

Title from element: 0 hello
Facebook number from element 0 4
Twitter number from element 0 6
Title from element: 1 hello
Facebook number from element 1 1
Twitter number from element 1 3
Title from element 0 and embedded object index 0: hello
Facebook number from element 0 and embedded object index 0: 4
Twitter number from element 0 and embedded object index 0: 6
Title from element 1 and embedded object index 0: hello
Facebook number from element 1 and embedded object index 0: 1
Twitter number from element 1 and embedded object index 0: 3

Upvotes: 0

fditz
fditz

Reputation: 882

Try using json_decode() function (php manual entry). In this way you can easily access the values of a json and avoid headaches on trying to find the values on a array.

This will also return NULL if your Json is invalid or have a problem (which helps to debug)

Upvotes: 0

JimL
JimL

Reputation: 2541

Your json is invalid.

http://php.net/manual/en/function.json-last-error.php

$json = '[{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":4,
  "twitter":6}
  }],

  [{"type":"article",
  "article":[{
  "title":"hello",
  "number":{
  "facebook":1,
  "twitter":3}
  }]';
$array = json_decode($json, true);

switch (json_last_error()) {
  case JSON_ERROR_NONE:
    echo ' - No errors';
    break;
  case JSON_ERROR_DEPTH:
    echo ' - Maximum stack depth exceeded';
    break;
  case JSON_ERROR_STATE_MISMATCH:
    echo ' - Underflow or the modes mismatch';
    break;
  case JSON_ERROR_CTRL_CHAR:
    echo ' - Unexpected control character found';
    break;
  case JSON_ERROR_SYNTAX:
    echo ' - Syntax error, malformed JSON';
    break;
  case JSON_ERROR_UTF8:
    echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
  default:
    echo ' - Unknown error';
    break;
}

print_r($array);
  • Syntax error, malformed JSON

Upvotes: 2

Related Questions