Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [json][php] odczyt pliku json i wyświetlenie w php
Forum PHP.pl > Forum > Przedszkole
sznoorek
Witam!
Potrzebuje pomocy, chce pobrać dane z pliku który z tego co wiem jest w formacie json.
Zawartośc zdalnego pliku:
Kod
{
  "status": "ok",
  "status_code": "NO_ERROR",
  "data": {
    "achievements": {
      "medalCarius": 3,
      "medalHalonen": 0,
      "medalPascucci": 0,
      "invader": 0,
      "medalFadin": 0,
      "armorPiercer": 1,
      "medalEkins": 0,
      "mousebane": 0,
      "mechanicEngineer": false,
      "medalBrunoPietro": 0,
      "heroesOfRassenay": 0,
      "medalKay": 4,
      "evileye": 0,
      "tankExperts": {
        "usa": false,
        "france": false,
        "ussr": false,
        "china": false,
        "uk": false,
        "germany": false
      },
      "defender": 1,
      "medalLeClerc": 3,
      "supporter": 0,
      "medalTamadaYoshio": 0,
      "steelwall": 0,
      "bombardier": 0,
      "medalAbrams": 3,
      "medalBrothersInArms": 0,
      "maxDiehardSeries": 3,
      "medalPoppel": 3,
      "medalOrlik": 0,
      "maxKillingSeries": 4,
      "handOfDeath": 1,
      "medalTarczay": 0,
      "sinai": 0,
      "sniper": 1,
      "warrior": 0,
      "titleSniper": 1,
      "maxInvincibleSeries": 3,
      "medalCrucialContribution": 0,
      "medalDeLanglade": 0,
      "medalWittmann": 0,
      "medalBurda": 0,
      "maxPiercingSeries": 9,
      "scout": 0,
      "beasthunter": 0,
      "kamikaze": 0,
      "medalRadleyWalters": 0,
      "raider": 0,
      "medalNikolas": 0,
      "medalOskin": 0,
      "medalBillotte": 0,
      "medalLavrinenko": 4,
      "medalKolobanov": 0,
      "invincible": 0,
      "lumberjack": 0,
      "medalBoelter": 0,
      "tankExpert": 0,
      "diehard": 0,
      "medalLafayettePool": 0,
      "mechanicEngineers": {
        "usa": false,
        "france": false,
        "ussr": false,
        "china": false,
        "uk": false,
        "germany": false
      },
      "medalLehvaslaiho": 0,
      "medalDumitru": 0,
      "maxSniperSeries": 17,
      "medalKnispel": 3
    },
    "ratings": {
      "spotted": {
        "place": 759270,
        "value": 406
      },
      "dropped_ctf_points": {
        "place": 945790,
        "value": 159
      },
      "battle_avg_xp": {
        "place": 555893,
        "value": 209
      },
      "xp": {
        "place": 771174,
        "value": 92787
      },
      "battles": {
        "place": 839663,
        "value": 444
      },
      "damage_dealt": {
        "place": 733203,
        "value": 88326
      },
      "ctf_points": {
        "place": 884903,
        "value": 529
      },
      "integrated_rating": {
        "place": 697870,
        "value": 4
      },
      "battle_avg_performance": {
        "place": 1088643,
        "value": 45
      },
      "frags": {
        "place": 783394,
        "value": 231
      },
      "battle_wins": {
        "place": 854002,
        "value": 200
      }
    },
    "name": "Neomagic",
    "created_at": 1327841290.0,
    "vehicles": [
      {
        "spotted": 0,
        "localized_name": "SU-85B",
        "name": "GAZ-74b",
        "level": 4,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 106,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-gaz-74b.png",
        "frags": 0,
        "win_count": 56,
        "class": "AT-SPG"
      },
      {
        "spotted": 0,
        "localized_name": "T-28",
        "name": "T-28",
        "level": 4,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 94,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-28.png",
        "frags": 0,
        "win_count": 42,
        "class": "mediumTank"
      },
      {
        "spotted": 0,
        "localized_name": "KW-1",
        "name": "KV1",
        "level": 5,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 75,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-kv1.png",
        "frags": 0,
        "win_count": 24,
        "class": "heavyTank"
      },
      {
        "spotted": 0,
        "localized_name": "T-46",
        "name": "T-46",
        "level": 3,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 45,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-46.png",
        "frags": 0,
        "win_count": 21,
        "class": "lightTank"
      },
      {
        "spotted": 0,
        "localized_name": "SU-76",
        "name": "SU-76",
        "level": 3,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 31,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-su-76.png",
        "frags": 0,
        "win_count": 16,
        "class": "AT-SPG"
      },
      {
        "spotted": 0,
        "localized_name": "T-50",
        "name": "T-50",
        "level": 4,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 31,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-50.png",
        "frags": 0,
        "win_count": 14,
        "class": "lightTank"
      },
      {
        "spotted": 0,
        "localized_name": "SU-85",
        "name": "SU-85",
        "level": 5,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 25,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-su-85.png",
        "frags": 0,
        "win_count": 9,
        "class": "AT-SPG"
      },
      {
        "spotted": 0,
        "localized_name": "KW-2",
        "name": "KV2",
        "level": 6,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 20,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-kv2.png",
        "frags": 0,
        "win_count": 10,
        "class": "heavyTank"
      },
      {
        "spotted": 0,
        "localized_name": "T-26",
        "name": "T-26",
        "level": 2,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 8,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-26.png",
        "frags": 0,
        "win_count": 4,
        "class": "lightTank"
      },
      {
        "spotted": 0,
        "localized_name": "AT-1",
        "name": "AT-1",
        "level": 2,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 6,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-at-1.png",
        "frags": 0,
        "win_count": 3,
        "class": "AT-SPG"
      },
      {
        "spotted": 0,
        "localized_name": "MS-1",
        "name": "MS-1",
        "level": 1,
        "damageDealt": 0,
        "survivedBattles": 0,
        "battle_count": 3,
        "nation": "ussr",
        "image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-ms-1.png",
        "frags": 0,
        "win_count": 1,
        "class": "lightTank"
      }
    ],
    "updated_at": 1352126066.0,
    "battles": {
      "spotted": 406,
      "hits_percents": 48,
      "capture_points": 529,
      "damage_dealt": 88326,
      "frags": 231,
      "dropped_capture_points": 159
    },
    "summary": {
      "wins": 200,
      "losses": 237,
      "battles_count": 444,
      "survived_battles": 74
    },
    "experience": {
      "xp": 92787,
      "battle_avg_xp": 209,
      "max_xp": 1107
    },
    "clan": {
      "member": null,
      "clan": null,
      "clan_ext": null
    }
  }
}


Dane które będą pobranę chce nałożyć na obrazek (PHP + GD).

Niestety nie miałem żadnego kontaktu z json, który jest porównywalny do xml'a.
Jakby ktoś dał przykład w jaki sposób pobrać dane typu:
Kod
"place": 759270,
"value": 406
"spotted": 0,
"localized_name": "T-50",
"name": "T-50",
"level": 4,
"damageDealt": 0,
"survivedBattles": 0,
"battle_count": 31,
"nation": "ussr",
"image_url": "/static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-50.png",
"frags": 0,
"win_count": 14,
"class": "lightTank"


Z góry dziekuję, nawet za nakierownia jak to zrobić.
Aktualnie mam skrypt PHP który wyszukje te dane z html'a, ale nie wszytskie dane są tam pokazywane.
tab
ja uzywam jsona do pobierania danych ajaxem, np:

skrypt.php
  1. $godzina = date('H:i');
  2. echo json_encode($godzina);


jakis inny plik, niekoniecznie js
[JAVASCRIPT] pobierz, plaintext
  1. $.ajax({
  2. type : "POST",
  3. url : "skrypt.php",
  4. dataType : "json",
  5. data : {},
  6. success (json)
  7. {
  8. alert(json['godzina']);
  9. }
  10. });
[JAVASCRIPT] pobierz, plaintext


przyznam ze nei interesowalem sie jak ich uzywac w php, ale mysle ze po prostu przelec po stringu z uzyciem explode albo cos podobnego.. zalezy ktorych danych potrzebujesz i co dokladnie zamierzasz z nimi zrobic
Szymciosek
Każda klamra to kolejny array, więc mając np coś takiego:

{ "data": { "place": 759270, "value": 406... } }

pobierasz sobie json skąd tam masz i możesz zrobić

  1. $json->place; //output: 759270
Piogola
  1. $tablica = json_decode(file_get_contents('plik_json.txt'));
sznoorek
traktując to print_r ($tablica); otrzymuje
  1. stdClass Object ( [status] => ok [status_code] => NO_ERROR [data] => stdClass Object ( [achievements] => stdClass Object ( [medalCarius] => 2 [medalHalonen] => 0 [medalPascucci] => 0 [invader] => 1 [medalFadin] => 0 [armorPiercer] => 1 [medalEkins] => 4 [mousebane] => 0 [mechanicEngineer] => [medalBrunoPietro] => 0 [heroesOfRassenay] => 0 [medalKay] => 3 [evileye] => 0 [tankExperts] => stdClass Object ( [usa] => [france] => [ussr] => [china] => [uk] => [germany] => ) [defender] => 1 [medalLeClerc] => 2 [supporter] => 5 [medalTamadaYoshio] => 0 [steelwall] => 2 [bombardier] => 0 [medalAbrams] => 3 [medalBrothersInArms] => 0 [maxDiehardSeries] => 6 [medalPoppel] => 3 [medalOrlik] => 0 [maxKillingSeries] => 5 [handOfDeath] => 1 [medalTarczay] => 0 [sinai] => 0 [sniper] => 10 [warrior] => 7 [titleSniper] => 1 [maxInvincibleSeries] => 3 [medalCrucialContribution] => 0 [medalDeLanglade] => 0 [medalWittmann] => 0 [medalBurda] => 0 [maxPiercingSeries] => 20 [scout] => 3 [beasthunter] => 0 [kamikaze] => 0 [medalRadleyWalters] => 0 [raider] => 0 [medalNikolas] => 0 [medalOskin] => 0 [medalBillotte] => 0 [medalLavrinenko] => 3 [medalKolobanov] => 0 [invincible] => 0 [lumberjack] => 0 [medalBoelter] => 0 [tankExpert] => 0 [diehard] => 0 [medalLafayettePool] => 0 [mechanicEngineers] => stdClass Object ( [usa] => [france] => [ussr] => [china] => [uk] => [germany] => ) [medalLehvaslaiho] => 0 [medalDumitru] => 0 [maxSniperSeries] => 31 [medalKnispel] => 3 ) [ratings] => stdClass Object ( [spotted] => stdClass Object ( [place] => 395729 [value] => 1563 ) [dropped_ctf_points] => stdClass Object ( [place] => 606797 [value] => 539 ) [battle_avg_xp] => stdClass Object ( [place] => 242435 [value] => 327 ) [xp] => stdClass Object ( [place] => 436802 [value] => 408360 ) [battles] => stdClass Object ( [place] => 530672 [value] => 1250 ) [damage_dealt] => stdClass Object ( [place] => 442099 [value] => 449078 ) [ctf_points] => stdClass Object ( [place] => 392365 [value] => 3040 ) [integrated_rating] => stdClass Object ( [place] => 363942 [value] => 8 ) [battle_avg_performance] => stdClass Object ( [place] => 96398 [value] => 53 ) [frags] => stdClass Object ( [place] => 402506 [value] => 1094 ) [battle_wins] => stdClass Object ( [place] => 501087 [value] => 659 ) ) [name] => Lector [created_at] => 1302624421 [vehicles] => Array ( [0] => stdClass Object ( [spotted] => 0 [localized_name] => SU-85 [name] => SU-85 [level] => 5 [damageDealt] => 0 [survivedBattles] => 0 [battle_count] => 142 [nation] => ussr [image_url] => /static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-su-85.png [frags] => 0 [win_count] => 80 [class] => AT-SPG ) [1] => stdClass Object ( [spotted] => 0 [localized_name] => M10 Wolverine [name] => M10_Wolverine [level] => 5 [damageDealt] => 0 [survivedBattles] => 0 [battle_count] => 123 [nation] => usa [image_url] => /static/2.1.2/encyclopedia/tankopedia/vehicle/small/usa-m10_wolverine.png [frags] => 0 [win_count] => 73 [class] => AT-SPG ) [2] => stdClass Object ( [spotted] => 0 [localized_name] => T-34 [name] => T-34 [level] => 5 [damageDealt] => 0 [survivedBattles] => 0 [battle_count] => 117 [nation] => ussr [image_url] => /static/2.1.2/encyclopedia/tankopedia/vehicle/small/ussr-t-34.png itd

Ale jak się dobrać do taliej tablicy?
Szymciosek
Już Ci pisałem mniej więcej, najpierw pobaw się z tym co ja Ci dałem.
Szymciosek
W głowę się puknij może zadziała, a nie jakieś śmieci wypisujesz...
sznoorek
Dałem tak:
  1. <?php
  2. if(!function_exists("curl_init")) die("cURL nie jest zainstalowany");
  3.  
  4. $url = 'plik.json';
  5. $curl = curl_init();
  6. curl_setopt( $curl, CURLOPT_URL, $url );
  7. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
  8. $result = curl_exec( $curl );
  9. curl_close( $curl );
  10. $return = json_decode($result, true);
  11.  
  12. print_r ($return);
  13. ?>

i teraz pod $return mam tablice.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.