Odczyt danych json

Odpowiedz Nowy wątek
2019-08-14 09:50
0

Witam od kilku dni mam problem z poprawnym odczytaniem danych zapisanych w formacie json. Sposoby z internetu mi nie działają.
Poniżej fragment pliku json. Udało mi się wyświetlić wszystkie dane używając php ale mnie interesuje jak wyświetlić 1 konkretny element w tym wypadku np. clan_tag.
{
"status": "ok",
"meta": {
"count": 10
},
"data": [
{
"gm_elo_rating_10": {
"rank_delta": 0,
"rank": 1,
"value": 1741
},
"gm_elo_rating_8": {
"rank_delta": null,
"rank": null,
"value": 1634
},
"fb_elo_rating_8": {
"rank_delta": 0,
"rank": 4,
"value": 1662
},
"fb_elo_rating_6": {
"rank_delta": 1,
"rank": 7,
"value": 1485
},
"exclude_reasons": {
"gm_elo_rating_8": "inactivity",
"gm_elo_rating_6": "inactivity"
},
"efficiency": {
"rank_delta": 0,
"rank": 9,
"value": 14142
},
"rating_fort": {
"rank_delta": 0,
"rank": 2,
"value": 3749.44
},
"fb_elo_rating_10": {
"rank_delta": 0,
"rank": 1,
"value": 1754
},
"clan_tag": "FAME",
"battles_count_avg_daily": {
"rank_delta": -11,
"rank": 94,
"value": 17.5
},
"global_rating_weighted_avg": {
"rank_delta": 9,
"rank": 149,
"value": 6432.8
},
"clan_id": 500025989,
"fb_elo_rating": {
"rank_delta": -1,
"rank": 5,
"value": 1945.4
},
"gm_elo_rating_6": {
"rank_delta": null,
"rank": null,
"value": 1514
},
"wins_ratio_avg": {
"rank_delta": 0,
"rank": 1,
"value": 66.16
},
"clan_name": "Deal with it!",
"gm_elo_rating": {
"rank_delta": 0,
"rank": 1,
"value": 2611.5
},
"battles_count_avg": {
"rank_delta": -8,
"rank": 43,
"value": 39962.1
},
"v10l_avg": {
"rank_delta": -3,
"rank": 8,
"value": 34.95
},
"global_rating_avg": {
"rank_delta": 0,
"rank": 1,
"value": 10903.05
}
},
{
"gm_elo_rating_10": {
"rank_delta": null,
"rank": null,
"value": 1000
},
"gm_elo_rating_8": {
"rank_delta": null,
"rank": null,
"value": 1000
},
"fb_elo_rating_8": {
"rank_delta": null,
"rank": null,
"value": 1010
},
"fb_elo_rating_6": {
"rank_delta": null,
"rank": null,
"value": 1170
},
"exclude_reasons": {
"gm_elo_rating_8": "limits",
"fort_elo_rating_6": "inactivity",
"fort_elo_rating_8": "limits",
"fort_rating": "limits",
"fort_elo_rating_10": "limits",
"fort_elo_rating": "limits",
"gm_elo_rating": "limits",
"gm_elo_rating_6": "limits",
"gm_elo_rating_10": "limits"
},
"efficiency": {
"rank_delta": -28,
"rank": 2084,
"value": 5212
},
"rating_fort": {
"rank_delta": null,
"rank": null,
"value": 0
},
"fb_elo_rating_10": {
"rank_delta": null,
"rank": null,
"value": 1000
},
"clan_tag": "B_M_G",
"battles_count_avg_daily": {
"rank_delta": 11,
"rank": 761,
"value": 13.49
},
"global_rating_weighted_avg": {
"rank_delta": -2,
"rank": 742,
"value": 4072.51
},
"clan_id": 500048438,
"fb_elo_rating": {
"rank_delta": null,
"rank": null,
"value": 0
},
"gm_elo_rating_6": {
"rank_delta": null,
"rank": null,
"value": 969
},
"wins_ratio_avg": {
"rank_delta": 0,
"rank": 2,
"value": 62.38
},
"clan_name": "Black Monks Guild",
"gm_elo_rating": {
"rank_delta": null,
"rank": null,
"value": 0
},
"battles_count_avg": {
"rank_delta": 2,
"rank": 1004,
"value": 26406.82
},
"v10l_avg": {
"rank_delta": -9,
"rank": 8533,
"value": 3.04
},
"global_rating_avg": {
"rank_delta": -2,
"rank": 469,
"value": 7144.75
}
},
{
"gm_elo_rating_10": {
"rank_delta": 2,
"rank": 7,
"value": 1573
},
"gm_elo_rating_8": {
"rank_delta": null,
"rank": null,
"value": 1316
},
"fb_elo_rating_8": {
"rank_delta": 0,
"rank": 5,
"value": 1638
},
"fb_elo_rating_6": {
"rank_delta": 0,
"rank": 1,
"value": 1581
},
"exclude_reasons": {
"gm_elo_rating_8": "inactivity",
"gm_elo_rating_6": "inactivity"
},
"efficiency": {
"rank_delta": -1,
"rank": 63,
"value": 13186
},
"rating_fort": {
"rank_delta": 1,
"rank": 133,
"value": 2938.85
},
"fb_elo_rating_10": {
"rank_delta": 0,
"rank": 4,
"value": 1701
},
"clan_tag": "MVPS",
"battles_count_avg_daily": {
"rank_delta": 5,
"rank": 69,
"value": 17.84
},
"global_rating_weighted_avg": {
"rank_delta": 6,
"rank": 214,
"value": 5982.63
},
"clan_id": 500023758,
"fb_elo_rating": {
"rank_delta": -6,
"rank": 149,
"value": 1507.37
},
"gm_elo_rating_6": {
"rank_delta": null,
"rank": null,
"value": 1278
},
"wins_ratio_avg": {
"rank_delta": 0,
"rank": 3,
"value": 62.28
},
"clan_name": "0:2 We lost, just go next!",
"gm_elo_rating": {
"rank_delta": 2,
"rank": 7,
"value": 2359.5
},
"battles_count_avg": {
"rank_delta": 2,
"rank": 149,
"value": 35452.63
},
"v10l_avg": {
"rank_delta": 4,
"rank": 89,
"value": 26.4
},
"global_rating_avg": {
"rank_delta": 2,
"rank": 41,
"value": 8929.3
}
},
{
"gm_elo_rating_10": {
"rank_delta": null,
"rank": 16,
"value": 1454
},
"gm_elo_rating_8": {
"rank_delta": null,
"rank": null,
"value": 1429
},
"fb_elo_rating_8": {
"rank_delta": 2,
"rank": 12,
"value": 1513
},
"fb_elo_rating_6": {
"rank_delta": -1,
"rank": 44,
"value": 1362
},
"exclude_reasons": {
"gm_elo_rating_8": "inactivity",
"gm_elo_rating_6": "inactivity"
},
"efficiency": {
"rank_delta": 132,
"rank": 18,
"value": 13918
},
"rating_fort": {
"rank_delta": -1,
"rank": 119,
"value": 2985.8
},
"fb_elo_rating_10": {
"rank_delta": -1,
"rank": 9,
"value": 1601
},
"clan_tag": "IDEAL",
"battles_count_avg_daily": {
"rank_delta": 47,
"rank": 329,
"value": 15.34
},
"global_rating_weighted_avg": {
"rank_delta": 0,
"rank": 8,
"value": 9062.73
},
"clan_id": 500010805,
"fb_elo_rating": {
"rank_delta": 0,
"rank": 94,
"value": 1598.12
},
Plik php

<?php
$tabb = file_get_contents('dane.json');
 $json = json_decode($tabb, true);

    var_dump($json);

?>

Pozostało 580 znaków

2019-08-14 10:15
1

json_decode z parametrem true zawraca ci tablicę asocjacyjną, wszystko masz w dokumentacji.

Pozostało 580 znaków

2019-08-14 10:19
0

No i jak z tego wyciągnąć konkretny element?

Pozostało 580 znaków

2019-08-14 10:22
1

jeśli nie wiesz co to tablica i jak się jej używa wróć do podstaw, takie coś poznajesz na 3 lekcji w technikum, pierwszych zajęciach na studiach i pierwszej części każdego kursu/tutorialu

Pozostało 580 znaków

2019-08-14 11:00

Przeanalizuj sobie (albo wklej np.tutaj):

<?php

// Twój poprawiony JSON...
        $json = '{
  "status": "ok",
  "meta": {
    "count": 10
  },
  "data": [
    {
      "gm_elo_rating_10": {
        "rank_delta": 0,
        "rank": 1,
        "value": 1741
      },
      "gm_elo_rating_8": {
        "rank_delta": null,
        "rank": null,
        "value": 1634
      },
      "fb_elo_rating_8": {
        "rank_delta": 0,
        "rank": 4,
        "value": 1662
      },
      "fb_elo_rating_6": {
        "rank_delta": 1,
        "rank": 7,
        "value": 1485
      },
      "exclude_reasons": {
        "gm_elo_rating_8": "inactivity",
        "gm_elo_rating_6": "inactivity"
      },
      "efficiency": {
        "rank_delta": 0,
        "rank": 9,
        "value": 14142
      },
      "rating_fort": {
        "rank_delta": 0,
        "rank": 2,
        "value": 3749.44
      },
      "fb_elo_rating_10": {
        "rank_delta": 0,
        "rank": 1,
        "value": 1754
      },
      "clan_tag": "FAME",
      "battles_count_avg_daily": {
        "rank_delta": -11,
        "rank": 94,
        "value": 17.5
      },
      "global_rating_weighted_avg": {
        "rank_delta": 9,
        "rank": 149,
        "value": 6432.8
      },
      "clan_id": 500025989,
      "fb_elo_rating": {
        "rank_delta": -1,
        "rank": 5,
        "value": 1945.4
      },
      "gm_elo_rating_6": {
        "rank_delta": null,
        "rank": null,
        "value": 1514
      },
      "wins_ratio_avg": {
        "rank_delta": 0,
        "rank": 1,
        "value": 66.16
      },
      "clan_name": "Deal with it!",
      "gm_elo_rating": {
        "rank_delta": 0,
        "rank": 1,
        "value": 2611.5
      },
      "battles_count_avg": {
        "rank_delta": -8,
        "rank": 43,
        "value": 39962.1
      },
      "v10l_avg": {
        "rank_delta": -3,
        "rank": 8,
        "value": 34.95
      },
      "global_rating_avg": {
        "rank_delta": 0,
        "rank": 1,
        "value": 10903.05
      }
    },
    {
      "gm_elo_rating_10": {
        "rank_delta": null,
        "rank": null,
        "value": 1000
      },
      "gm_elo_rating_8": {
        "rank_delta": null,
        "rank": null,
        "value": 1000
      },
      "fb_elo_rating_8": {
        "rank_delta": null,
        "rank": null,
        "value": 1010
      },
      "fb_elo_rating_6": {
        "rank_delta": null,
        "rank": null,
        "value": 1170
      },
      "exclude_reasons": {
        "gm_elo_rating_8": "limits",
        "fort_elo_rating_6": "inactivity",
        "fort_elo_rating_8": "limits",
        "fort_rating": "limits",
        "fort_elo_rating_10": "limits",
        "fort_elo_rating": "limits",
        "gm_elo_rating": "limits",
        "gm_elo_rating_6": "limits",
        "gm_elo_rating_10": "limits"
      },
      "efficiency": {
        "rank_delta": -28,
        "rank": 2084,
        "value": 5212
      },
      "rating_fort": {
        "rank_delta": null,
        "rank": null,
        "value": 0
      },
      "fb_elo_rating_10": {
        "rank_delta": null,
        "rank": null,
        "value": 1000
      },
      "clan_tag": "B_M_G",
      "battles_count_avg_daily": {
        "rank_delta": 11,
        "rank": 761,
        "value": 13.49
      },
      "global_rating_weighted_avg": {
        "rank_delta": -2,
        "rank": 742,
        "value": 4072.51
      },
      "clan_id": 500048438,
      "fb_elo_rating": {
        "rank_delta": null,
        "rank": null,
        "value": 0
      },
      "gm_elo_rating_6": {
        "rank_delta": null,
        "rank": null,
        "value": 969
      },
      "wins_ratio_avg": {
        "rank_delta": 0,
        "rank": 2,
        "value": 62.38
      },
      "clan_name": "Black Monks Guild",
      "gm_elo_rating": {
        "rank_delta": null,
        "rank": null,
        "value": 0
      },
      "battles_count_avg": {
        "rank_delta": 2,
        "rank": 1004,
        "value": 26406.82
      },
      "v10l_avg": {
        "rank_delta": -9,
        "rank": 8533,
        "value": 3.04
      },
      "global_rating_avg": {
        "rank_delta": -2,
        "rank": 469,
        "value": 7144.75
      }
    },
    {
      "gm_elo_rating_10": {
        "rank_delta": 2,
        "rank": 7,
        "value": 1573
      },
      "gm_elo_rating_8": {
        "rank_delta": null,
        "rank": null,
        "value": 1316
      },
      "fb_elo_rating_8": {
        "rank_delta": 0,
        "rank": 5,
        "value": 1638
      },
      "fb_elo_rating_6": {
        "rank_delta": 0,
        "rank": 1,
        "value": 1581
      },
      "exclude_reasons": {
        "gm_elo_rating_8": "inactivity",
        "gm_elo_rating_6": "inactivity"
      },
      "efficiency": {
        "rank_delta": -1,
        "rank": 63,
        "value": 13186
      },
      "rating_fort": {
        "rank_delta": 1,
        "rank": 133,
        "value": 2938.85
      },
      "fb_elo_rating_10": {
        "rank_delta": 0,
        "rank": 4,
        "value": 1701
      },
      "clan_tag": "MVPS",
      "battles_count_avg_daily": {
        "rank_delta": 5,
        "rank": 69,
        "value": 17.84
      },
      "global_rating_weighted_avg": {
        "rank_delta": 6,
        "rank": 214,
        "value": 5982.63
      },
      "clan_id": 500023758,
      "fb_elo_rating": {
        "rank_delta": -6,
        "rank": 149,
        "value": 1507.37
      },
      "gm_elo_rating_6": {
        "rank_delta": null,
        "rank": null,
        "value": 1278
      },
      "wins_ratio_avg": {
        "rank_delta": 0,
        "rank": 3,
        "value": 62.28
      },
      "clan_name": "0:2 We lost, just go next!",
      "gm_elo_rating": {
        "rank_delta": 2,
        "rank": 7,
        "value": 2359.5
      },
      "battles_count_avg": {
        "rank_delta": 2,
        "rank": 149,
        "value": 35452.63
      },
      "v10l_avg": {
        "rank_delta": 4,
        "rank": 89,
        "value": 26.4
      },
      "global_rating_avg": {
        "rank_delta": 2,
        "rank": 41,
        "value": 8929.3
      }
    },
    {
      "gm_elo_rating_10": {
        "rank_delta": null,
        "rank": 16,
        "value": 1454
      },
      "gm_elo_rating_8": {
        "rank_delta": null,
        "rank": null,
        "value": 1429
      },
      "fb_elo_rating_8": {
        "rank_delta": 2,
        "rank": 12,
        "value": 1513
      },
      "fb_elo_rating_6": {
        "rank_delta": -1,
        "rank": 44,
        "value": 1362
      },
      "exclude_reasons": {
        "gm_elo_rating_8": "inactivity",
        "gm_elo_rating_6": "inactivity"
      },
      "efficiency": {
        "rank_delta": 132,
        "rank": 18,
        "value": 13918
      },
      "rating_fort": {
        "rank_delta": -1,
        "rank": 119,
        "value": 2985.8
      },
      "fb_elo_rating_10": {
        "rank_delta": -1,
        "rank": 9,
        "value": 1601
      },
      "clan_tag": "IDEAL",
      "battles_count_avg_daily": {
        "rank_delta": 47,
        "rank": 329,
        "value": 15.34
      },
      "global_rating_weighted_avg": {
        "rank_delta": 0,
        "rank": 8,
        "value": 9062.73
      },
      "clan_id": 500010805,
      "fb_elo_rating": {
        "rank_delta": 0,
        "rank": 94,
        "value": 1598.12
      }
    }
  ]
}';

// dekodujesz do postaci tablicy, a nie obiektu
$decodedJson = json_decode($json, true);

// widać, że data ma w sobie 4 tablice z takimi samymi indeksami - wyniki dla danych osób jak mniemam
var_dump($decodedJson['data'][1]['clan_tag']);

// dla każdego elementu, który jest pod indeksem "data" ` $decodedJson pokaż wartość dla indeksu "clean_tag"
foreach ($decodedJson['data'] as $person) {
    var_dump($person['clan_tag']);
}
die;

To tyle młody padawanie pehapca ;)

edytowany 1x, ostatnio: Pipes, 2019-08-14 11:01
Wielkie dzięki, teraz też sam próbowałem i widzę że byłem bardzo blisko. - akinet 2019-08-14 11:09

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0