Probleme mit Auslesen der json-Datei
lima-city → Forum → Programmiersprachen → PHP, MySQL & .htaccess
-
Hallo, nochmal,
da ich diesen Faden weder hochholen, noch meinen letzten Beitrag editieren darf, muss ich einen neuen aufmachen. @Moderator: ggf. den neuen Faden mit diesem hier zusammenlegen: https://www.lima-city.de/thread/externe-json-url-in-txt-datei-umwandeln
Es ist nämlich noch ein neues Problem aufgetaucht, was ich nicht verstehe. So treten bei strukturell eigentlich identischen json-Dateien unterschiedliche Ergebnisse zutage.
Während es hier funktioniert...
<?php ini_set('display_errors', 1); error_reporting(E_ALL); $url = 'https://www.mdr.de/XML/titellisten/jump_onair.json'; $contents = file_get_contents($url); $contents = utf8_encode($contents); $results = json_decode($contents); $song = $results->{'Songs'}->{'0'}->{'interpret'}." - ".$results->{'Songs'}->{'0'}->{'title'}; print( $song ); ?>
...kommt beim nachfolgenden Code die Fehlermeldung
Notice: Trying to get property of non-object in /home/webpages/lima-city/kurzmitteilungsservice/html/radioberlin/webradiotest/webradiopunktcc/ch/titel/fm1titel.php on line 9
<?php ini_set('display_errors', 1); error_reporting(E_ALL); $url = 'https://songinfo.fm1today.ch/FM1N/fm1n-current.json'; $contents = file_get_contents($url); $contents = utf8_encode($contents); $results = json_decode($contents); $song = $results->{'live'}->{'0'}->{'interpret'}." - ".$results->{'live'}->{'0'}->{'title'}; print( $song ); ?>
-
Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!
lima-city: Gratis werbefreier Webspace für deine eigene Homepage
-
Wie kommst du auf Faden, es ist ein Thema
Identisch vom Aufbau scheinen die jsons nicht zu sein:
{ "Resulttype":"OK", "Info":"", "Songs":{ "0":{ "status":"now", "id_titel":"EA36943B", "title":"Power Over Me", "subtitle":"", "starttime":"2019-02-10 13:17:51", "author":"Dermot Kennedy", "av_id":"0", "av_next_id":"0", "duration":"00:03:26", "interpret":"Dermot Kennedy", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"Dermot Kennedy", "label":"Universal", "tontraeger":"UMI New Releases - KW 42" }, "1":{ "status":"old", "id_titel":"6F835C10", "title":"One Kiss", "subtitle":"", "starttime":"2019-02-10 13:14:42", "author":"Harris, Calvin", "av_id":"0", "av_next_id":"0", "duration":"00:03:30", "interpret":"Calvin Harris", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ { "@attributes":{ "name":"variantSmall16x9", "width":"512", "height":"288", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/calvin-harris-132-resimage_v-variantSmall16x9_w-512.jpg?version=36663" } }, { "@attributes":{ "name":"variantBig16x9", "width":"960", "height":"540", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/calvin-harris-132-resimage_v-variantBig16x9_w-960.jpg?version=36663" } }, { "@attributes":{ "name":"variantSmall1x1", "width":"512", "height":"512", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/calvin-harris-132-resimage_v-variantSmall1x1_w-512.jpg?version=36663" } }, { "@attributes":{ "name":"variantBig1x1", "width":"960", "height":"960", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/calvin-harris-132-resimage_v-variantBig1x1_w-960.jpg?version=36663" } } ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"Adam Wiles", "label":"Columbia", "tontraeger":"One Kiss" }, "2":{ "status":"old", "id_titel":"80BDA5A2", "title":"Billie Jean", "subtitle":"", "starttime":"2019-02-10 13:09:06", "author":"Jackson, Michael", "av_id":"0", "av_next_id":"0", "duration":"00:04:52", "interpret":"Michael Jackson", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ { "@attributes":{ "name":"variantSmall16x9", "width":"512", "height":"288", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/interpret148-resimage_v-variantSmall16x9_w-512.jpg?version=10310" } }, { "@attributes":{ "name":"variantBig16x9", "width":"960", "height":"540", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/interpret148-resimage_v-variantBig16x9_w-960.jpg?version=10310" } }, { "@attributes":{ "name":"variantSmall1x1", "width":"512", "height":"512", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/interpret148-resimage_v-variantSmall1x1_w-512.jpg?version=10310" } }, { "@attributes":{ "name":"variantBig1x1", "width":"960", "height":"960", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/interpret148-resimage_v-variantBig1x1_w-960.jpg?version=10310" } } ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"Jackson, Michael", "label":"Columbia", "tontraeger":"" }, "3":{ "status":"old", "id_titel":"F967BEA0", "title":"Shotgun", "subtitle":"", "starttime":"2019-02-10 13:06:06", "author":"George Ezra", "av_id":"0", "av_next_id":"0", "duration":"00:03:11", "interpret":"George Ezra", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ { "@attributes":{ "name":"variantSmall16x9", "width":"512", "height":"288", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/ezra102-resimage_v-variantSmall16x9_w-512.jpg?version=4899" } }, { "@attributes":{ "name":"variantBig16x9", "width":"960", "height":"540", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/ezra102-resimage_v-variantBig16x9_w-960.jpg?version=4899" } }, { "@attributes":{ "name":"variantSmall1x1", "width":"512", "height":"512", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/ezra102-resimage_v-variantSmall1x1_w-512.jpg?version=4899" } } ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"George Barnett", "label":"Columbia", "tontraeger":"Shotgun" }, "4":{ "status":"old", "id_titel":"6EEEE3CD", "title":"Sido - Tausend Tattoos", "subtitle":"", "starttime":"2019-02-10 12:58:44", "author":"Sido", "av_id":"0", "av_next_id":"0", "duration":"00:00:42", "interpret":"Sido", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ { "@attributes":{ "name":"variantSmall16x9", "width":"512", "height":"288", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/sido104-resimage_v-variantSmall16x9_w-512.jpg?version=53134" } }, { "@attributes":{ "name":"variantBig16x9", "width":"960", "height":"540", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/sido104-resimage_v-variantBig16x9_w-960.jpg?version=53134" } }, { "@attributes":{ "name":"variantSmall1x1", "width":"512", "height":"512", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/sido104-resimage_v-variantSmall1x1_w-512.jpg?version=53134" } }, { "@attributes":{ "name":"variantBig1x1", "width":"960", "height":"960", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/sido104-resimage_v-variantBig1x1_w-960.jpg?version=53134" } } ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"Sido Gold", "label":"Urban", "tontraeger":"Tausend Tattoos" }, "5":{ "status":"old", "id_titel":"0CA0CAA2", "title":"More Than You Know", "subtitle":"", "starttime":"2019-02-10 12:55:24", "author":"Axwell \u0026 Ingrosso", "av_id":"0", "av_next_id":"0", "duration":"00:03:11", "interpret":"Axwell \u0026 Ingrosso", "kurzinfo":"", "metadatentext":"", "interpret_url":"", "artist_image_id":{ "imageVariant":[ { "@attributes":{ "name":"variantSmall16x9", "width":"512", "height":"288", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/axwell-ingrosso-104-resimage_v-variantSmall16x9_w-512.jpg?version=12976" } }, { "@attributes":{ "name":"variantBig16x9", "width":"960", "height":"540", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/axwell-ingrosso-104-resimage_v-variantBig16x9_w-960.jpg?version=12976" } }, { "@attributes":{ "name":"variantSmall1x1", "width":"512", "height":"512", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/axwell-ingrosso-104-resimage_v-variantSmall1x1_w-512.jpg?version=12976" } }, { "@attributes":{ "name":"variantBig1x1", "width":"960", "height":"960", "mimeType":"image/jpeg", "url":"https://www.jumpradio.de/musik/interpret/axwell-ingrosso-104-resimage_v-variantBig1x1_w-960.jpg?version=12976" } } ], "@root":"root" }, "transmissiontype":"offline", "audioasset":{ "asset":[ ], "@root":"root" }, "komponist":"Axel Hedfors", "label":"Virgin", "tontraeger":"More than you know" },
{ "coming":[ { "title":"We Must Be Crazy", "interpret":"Milow", "duration":"00:01", "label":"Vertigo Berlin", "playtime":"2019-02-10T13:27Z", "m3uURL":"https:\/\/www.radiofm1.ch\/streaming\/fm1sg128.m3u", "imageURL":"https:\/\/cover.fm1today.ch\/FM1N\/3651259.jpg" } ], "live":[ { "title":"Born To Be Yours", "interpret":"Kygo & Imagine Dragons", "duration":"03:04", "label":"SME International Limited UK", "playtime":"2019-02-10T13:21Z", "m3uURL":"https:\/\/www.radiofm1.ch\/streaming\/fm1sg128.m3u", "imageURL":"https:\/\/cover.fm1today.ch\/FM1N\/5215151.jpg" } ], "played":[ { "title":"Down Under", "interpret":"Men At Work", "duration":"03:18", "label":"", "playtime":"2019-02-10T13:21Z", "m3uURL":"https:\/\/www.radiofm1.ch\/streaming\/fm1sg128.m3u", "imageURL":"https:\/\/cover.fm1today.ch\/FM1N\/59584.jpg" } ] }
Eventuell siehst du den Unterschied. [,"Songs":{"0":{"status":] - ["live":[{"title":"Down Under",]
Kann leider nicht den ganzen formatierten Code dir rein packen, sollte aber eigentlich so passen:
Dieser "Text" kann doch nun wirklich nicht als neuer Beitrag durchgehen, oder? Bitte kürze auf das Wesentliche, kopiere keine fremden Texte!
Beitrag zuletzt geändert: 10.2.2019 13:39:26 von horstexplorer -
Hallo,
sollte so funktionieren, allerdings ungetestet...
$song = $results->{'live'}->{'interpret'}." - ".$results->{'live'}->{'title'};
-
OK, die Rohdaten geben tatsächlich...
{
"live": [
{
"title": "Smooth",
}
]
}
...aus. Die json wirft allerdings widersprüchlicherweise...
live
0
title "Smooth"
...aus.
@fits-radio: Der Verzicht auf die "0" führt zur gleichen Fehlermeldung. -
<?php ini_set('display_errors', 1); error_reporting(E_ALL); $url = 'https://songinfo.fm1today.ch/FM1N/fm1n-current.json'; $contents = file_get_contents($url); $contents = stripslashes(trim($contents)); if (substr($contents, 0, 3) == "\xef\xbb\xbf") { $contents = substr($contents, 3); } $contents = utf8_encode($contents); $results = json_decode($contents); $song = $results->{'live'}[0]->{'interpret'}." - ".$results->{'live'}[0]->{'title'}; print( $song ); ?>
Bisschen probiert, die Datei wird (manchmal) mit BOM gespeichert, welches (wenn vorhanden) in dem neuen Code entfernt wird. Desweiteren ist {'live'} ein Array.
Beitrag zuletzt geändert: 11.2.2019 23:48:14 von fits-radio -
OK, danke, die Titelanzeige funktioniert jetzt. Auf eine BOM-Codierung wäre ich nicht gekommen.
-
Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!
lima-city: Gratis werbefreier Webspace für deine eigene Homepage