Skip to content
Advertisement

Load JSON File and print specific Parts

im building a simple webpage for school. Sadly ive got several problem. Im using an Apache Webserver with XAMPP.

I got a JSON-File like:

{
    
"lose":[
{
    "Zustand":"geschlossen",
    "Losnummer":1,
    "Gewinnklasse":"A",
    "Preis":10
},

{
    "Zustand":"geschlossen",
    "Losnummer":2,
    "Gewinnklasse":"B",
    "Preis":20
},

This file is on my Webserver too. I want to load this file especially with XHTML Request and then want to print several parts of this JSON File into HTML/PHP Code. I watched a lot of youtube videos but i dont get a way that works for me. I managed to load the JSON-File via XHTML and im able to print it in HTML, but it is always the whole JSON in the so called “responseText” and this is basically a large String for me. Im fairly new in this stuff so sorry if i mess things up.

So TLDR: load JSON file in webpage, print single parts of it in several HTML div tags. Im allowed to use html,php,js.

Advertisement

Answer

Something like this:

<?php 
$json = 
'
{
    "lose": [
    {
        "Zustand":"geschlossen",
        "Losnummer":1,
        "Gewinnklasse":"A",
        "Preis":10
    },
    {
        "Zustand":"geschlossen",
        "Losnummer":2,
        "Gewinnklasse":"B",
        "Preis":20
    }]
}
';

$arr = json_decode($json, true);
echo "<table border='1'>";
foreach($arr["lose"] as $single) {
    echo "<tr>";
    echo "<td>".$single['Zustand']."</td>";
    echo "<td>".$single['Losnummer']."</td>";
    echo "</tr>";
}
echo "</table>";
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement