Extract items from Json

-1

How can I extract only the items from this json?

{
    "prueva": {
        "codCompeticion": "1",
        "nomCompeticion": "Juvenil_1",
        "jornadas": []
    },

    "codCompeticion": "2",
    "nomCompeticion": "Juvenil_2",
    "jornadas": [
    {
        "item1": "1",
        "item2": "2",
        "item3": "3"
    },
    {
        "item1": "4",
        "item2": "5",
        "item3": "6"
    }]
}

This is what I have but it does not come out

<?php

$calendario = 'http://juveniles.esy.es/2018/juvenil/aa.php';

$ch = curl_init($calendario);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data= curl_exec($ch);
curl_close($ch);

$objeto = json_decode($data, true);
$items = $objeto['prueva'][1]['jornadas'];

echo $items[1]['item2']; // 5

json_encode($items); // [{"item1":"1","item2":"2","item3":"3"},{"item1":"4","item2":"5","item3":"6"}]

echo $items;

?>
    
asked by Rafel C.F 15.09.2017 в 22:00
source

2 answers

0

There are some errors that do not occur because your PHP may have disabled the display_error property.

This location does not exist in your json :

$items = $objeto['prueva'][1]['jornadas'];//errado

You can not do echo to% array :

echo $items;//errado

SOLUTION:

<?php

$calendario = 'http://juveniles.esy.es/2018/juvenil/aa.php';   

$ch = curl_init($calendario);   
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$data= curl_exec($ch); 
curl_close($ch);  

$objeto = json_decode($data, true);
$items = $objeto['jornadas'];

echo $items[1]['item2']; // 5

echo json_encode($items); // [{"item1":"1","item2":"2","item3":"3"},{"item1":"4","item2":"5","item3":"6"}]

?>  
    
answered by 16.09.2017 / 08:19
source
1

Update

It is not necessary that you use the comments at the end of the lines, it was just an idea that gave you so that you knew what each variable has inside. If you only want to print the data of the items, you should remove all the lines with echo and leave only:

echo json_encode($items);

Original reply

The JSON you indicate is not a valid JSON. I guess it's because you're doing tests and it should really be something like:

{
    "prueva": [{
        "codCompeticion": "1",
        "nomCompeticion": "Juvenil_1",
        "jornadas": []
    },
    {
        "codCompeticion": "2",
        "nomCompeticion": "Juvenil_2",
        "jornadas": [
            {
                "item1": "1",
                "item2": "2",
                "item3": "3"
            },
            {
                "item1": "4",
                "item2": "5",
                "item3": "6"
            }
        ]
    }]
}

Now, assuming that the JSON is stored in a variable $json , I would do the following:

<?php
$objeto = json_decode($json, true);
$items = $objeto['prueva'][1]['jornadas'];
?>

And in the $items fix you would have the information you want, for example:

<?php
echo $items[1]['item2']; // 5
json_encode($items); // [{"item1":"1","item2":"2","item3":"3"},{"item1":"4","item2":"5","item3":"6"}]
?>
    
answered by 16.09.2017 в 00:26