-1

JSON ファイルからのデータのエコーに問題があります。

<?php
$url = file_get_contents("http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80");
$json = file_get_contents($url);
$data = json_decode($json, true);
echo "<pre>"; var_dump($data); echo"</pre>";
?>

上記は、私が使用している私のphpファイルで、以下に示すようにJSONを出力します:

{

    "id": 3121752,
    "name": "SnowderBlazer",
    "birth": "2010-04-10",
    "avatar": "http://static.erepublik.net/uploads/avatars/Citizens/2010/04/10/4bb9a72cc291faaaf7af8e78ed0a8509_100x100.jpg",
    "online": false,
    "ban": null,
    "alive": true,
    "level": 97,
    "experience": 360391,
    "strength": 42859.62,
    "rank": {
        "points": 437120237,
        "level": 64,
        "image": "http://www.erepublik.com/images/modules/ranks/god_of_war_2.png",
        "name": "God of War**"
    },
    "elite_citizen": false,
    "national_rank": 1,
    "residence": {
        "country": {
            "id": 65,
            "name": "Serbia",
            "code": "RS"
        },
        "region": {
            "id": 198,
            "name": "Midi-Pyrenees"
        }
    },
    "citizenship": {
        "id": 65,
        "name": "Serbia",
        "code": "RS"
    },
    "about": "Voters Club Moderator\ncatch me on #voters @Rizon\nIRC Nick : Snowderblazer OR Snowderblazer[BNC]\norder herehttp://erepublik-market.com/voters/newOrder.html?adp=1549866\n[ident:9vrwQZB9]",
    "party": {
        "id": 2479,
        "name": "Ujedinjena eSrbija",
        "role": "Party Member"
    },
    "army": {
        "id": 1980,
        "name": "Legija Stranaca Elite",
        "role": "Commander",
        "created_at": "2012-05-26",
        "avatar": "http://static.erepublik.net/uploads/avatars/Groups/2012/05/26/f80bf05527157a8c2a7bb63b22f49aaa_medium.jpg",
        "rank": "Commander"
    },
    "newspaper": {
        "id": 241367,
        "role": "Press director",
        "name": "M.A.D.S News"
    },
    "top_damage": {
        "damage": 215238312,
        "date": "2013-05-16",
        "message": "Achieved while successfully defending Basilicata against Italy on day 2,004"
    },
    "true_patriot": {
        "damage": 3021790429,
        "since": "2012-04-26"
    },
    "medals": {
        "battle_hero": 248,
        "campaign_hero": 98,
        "congress_member": 9,
        "country_president": 0,
        "hard_worker": 36,
        "media_mogul": 5,
        "mercenary": 1,
        "resistance_hero": 2,
        "society_builder": 0,
        "super_soldier": 171,
        "top_fighter": 3,
        "true_patriot": 43
    },
    "hit": 14924

}

私の問題は、すべてのデータを一度にロードすることだけであり、それぞれをエコーし​​たいだけです。JSON の経験がないため、エコーする変数がわかりません。

あなたがアクセスできるJSONを介して

JSON Version : http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80
XML Version : http://api.erpk.org/citizen/profile/3121752.xml?key=Yn3AsG80

私が達成したいことは次のとおりです。

  1. 出力なしで JSON または XML をロードして、ECHO を使用して、PHP ファイルで表示したい場所にデータを配置できるようにします。
  2. JSON または XML データのそれぞれを ECHO します。

試行するたびに、オブジェクトではないというエラーが表示されます

4

2 に答える 2

0

あなたは を誤用file_get_contentsしているので、それがあなたの問題だと思います。その URL からデータを取得するには、次の最初の行だけを使用します。

$url = 'http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80';

とにかく、JSON データセットを指定して短いプロファイルをエコーするコード スニペットを次に示します。

nameおよび のような第 1 レベルのフィールド、およびbirthのような第 2 レベルのフィールドrank\levelrank\nameそして最後に のようなフィールドの動的セットにアクセスする方法を示しますmedals

<?php
$json = '{"id":3121752,"name":"SnowderBlazer","birth":"2010-04-10","avatar":"http://static.erepublik.net/uploads/avatars/Citizens/2010/04/10/4bb9a72cc291faaaf7af8e78ed0a8509_100x100.jpg","online":false,"ban":null,"alive":true,"level":97,"experience":360391,"strength":42859.62,"rank":{"points":437120237,"level":64,"image":"http://www.erepublik.com/images/modules/ranks/god_of_war_2.png","name":"God of War**"},"elite_citizen":false,"national_rank":1,"residence":{"country":{"id":65,"name":"Serbia","code":"RS"},"region":{"id":198,"name":"Midi-Pyrenees"}},"citizenship":{"id":65,"name":"Serbia","code":"RS"},"about":"Voters Club Moderator\ncatch me on #voters @Rizon\nIRC Nick : Snowderblazer OR Snowderblazer[BNC]\norder herehttp://erepublik-market.com/voters/newOrder.html?adp=1549866\n[ident:9vrwQZB9]","party":{"id":2479,"name":"Ujedinjena eSrbija","role":"Party Member"},"army":{"id":1980,"name":"Legija Stranaca Elite","role":"Commander","created_at":"2012-05-26","avatar":"http://static.erepublik.net/uploads/avatars/Groups/2012/05/26/f80bf05527157a8c2a7bb63b22f49aaa_medium.jpg","rank":"Commander"},"newspaper":{"id":241367,"role":"Press director","name":"M.A.D.S News"},"top_damage":{"damage":215238312,"date":"2013-05-16","message":"Achieved while successfully defending Basilicata against Italy on day 2,004"},"true_patriot":{"damage":3021790429,"since":"2012-04-26"},"medals":{"battle_hero":248,"campaign_hero":98,"congress_member":9,"country_president":0,"hard_worker":36,"media_mogul":5,"mercenary":1,"resistance_hero":2,"society_builder":0,"super_soldier":171,"top_fighter":3,"true_patriot":43},"hit":14924}';

$data = json_decode($json, true);

echo "Name: {$data['name']}\n";
echo "Date of birth: {$data['birth']}\n\n";

echo "Level: {$data['rank']['level']}\n";
echo "Rank: {$data['rank']['name']}\n\n";

echo "Medals\n";
foreach ($data['medals'] as $medalName => $number) {
    echo ucfirst(str_replace('_', ' ', $medalName)) . ": {$number}\n";
}

出力

Name: SnowderBlazer
Date of birth: 2010-04-10

Level: 64
Rank: God of War**

Medals
Battle hero: 248
Campaign hero: 98
Congress member: 9
Country president: 0
Hard worker: 36
Media mogul: 5
Mercenary: 1
Resistance hero: 2
Society builder: 0
Super soldier: 171
Top fighter: 3
True patriot: 43
于 2013-05-27T19:44:39.077 に答える