JSON data echo

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Patrick B

Patrick B

28/05/2014 02:49:27
Quote Anchor link
Hey,

Ik heb hier json data

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[{"subcategory":"visual_arts",
"broadcast_part":2,
"featured":true,
"channel_subscription":false,
"id":"9700010016",
"category":"creativity",
"title":"Drawing: Mordesh! From WildStar.",
"channel_count":11,
"video_height":1152,
"site_count":0,
"embed_enabled":true,
"channel":{"category":"creativity",
"status":"Drawing: Mordesh! From WildStar.",
"subcategory":"visual_arts",
"language":"en",
"title":"foxberry_studios",
"channel_url":"http://www.justin.tv/foxberry_studios",
"producer":true,
"tags":null,
"subcategory_title":"Visual Arts",
"id":45251360,
"screen_cap_url_large":"http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-320x240.jpg",
"meta_game":"",
"mature":null,
"screen_cap_url_small":"http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-70x53.jpg",
"login":"foxberry_studios",
"timezone":"",
"screen_cap_url_medium":"http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-150x113.jpg",
"screen_cap_url_huge":"http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-630x473.jpg",
"category_title":"Creativity","views_count":612694},
"up_time":"Tue May 27 16:52:52 2014","format":"live",
"embed_count":0,
"stream_type":"live",
"abuse_reported":false,
"video_width":2048,
"geo":"US",
"name":"live_user_foxberry_studios",
"language":"en",
"stream_count":0,
"video_bitrate":1129.7109375,
"broadcaster":"obs",
"channel_view_count":0}]


En ik probeer bepaalde dingen aan een variable te plakken om later te kunnen printen / echo.
title, screen_cap_url_medium, channel_count, views_count etc

"title":"Drawing: Mordesh! From WildStar."
Voorbeeld :
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
$json_array = json_decode(file_get_contents('http://api.test.com/channel.json'), true);
$Title = $json_array['title'];
echo $Title;


Wil niet lukken, ik doe waarschijnlijk iets verkeerd.
Zou iemand mij kunnen helpen ?
Gewijzigd op 28/05/2014 02:54:41 door Patrick B
 
PHP hulp

PHP hulp

17/01/2022 02:58:56
 
Erwin H

Erwin H

28/05/2014 08:39:55
Quote Anchor link
Ten eerste heb je de json data structuur wat ongelukkig neergezet, waardoor je niet ziet dat 'channel' een object is en dat 'title' weer een property is van dat object:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[
    {
        "subcategory": "visual_arts",
        "broadcast_part": 2,
        "featured": true,
        "channel_subscription": false,
        "id": "9700010016",
        "category": "creativity",
        "title": "Drawing: Mordesh! From WildStar.",
        "channel_count": 11,
        "video_height": 1152,
        "site_count": 0,
        "embed_enabled": true,
        "channel": {
            "category": "creativity",
            "status": "Drawing: Mordesh! From WildStar.",
            "subcategory": "visual_arts",
            "language": "en",
            "title": "foxberry_studios",
            "channel_url": "http://www.justin.tv/foxberry_studios",
            "producer": true,
            "tags": null,
            "subcategory_title": "Visual Arts",
            "id": 45251360,
            "screen_cap_url_large": "http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-320x240.jpg",
            "meta_game": "",
            "mature": null,
            "screen_cap_url_small": "http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-70x53.jpg",
            "login": "foxberry_studios",
            "timezone": "",
            "screen_cap_url_medium": "http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-150x113.jpg",
            "screen_cap_url_huge": "http://static-cdn.jtvnw.net/previews-jtv/live_user_foxberry_studios-630x473.jpg",
            "category_title": "Creativity",
            "views_count": 612694
        },
        "up_time": "Tue May 27 16:52:52 2014",
        "format": "live",
        "embed_count": 0,
        "stream_type": "live",
        "abuse_reported": false,
        "video_width": 2048,
        "geo": "US",
        "name": "live_user_foxberry_studios",
        "language": "en",
        "stream_count": 0,
        "video_bitrate": 1129.7109375,
        "broadcaster": "obs",
        "channel_view_count": 0
    }
]

Je moet het dus een laag dieper zoeken:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
$Title
= $json_array['channel']['title'];
?>

Simpele manier om hier achter te komen is om na het omzetten van de json structuur gewoon even je array naar het scherm te printen
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php
echo '<pre>';
print_r( $json_array );
exit;
?>


P.S. Kan zelfs zo zijn dat je $Title = $json_array[0]['channel']['title']; moet gebruiken, maar probeer dat zelf even uit.
Gewijzigd op 28/05/2014 08:41:47 door Erwin H
 
Patrick B

Patrick B

28/05/2014 21:09:45
Quote Anchor link
Werkt perfect!
Die print_r( $json_array );
Is erg handig voor in de toekomst ;)

Thanks
Gewijzigd op 28/05/2014 21:10:16 door Patrick B
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.